Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created August 28, 2016 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/ff048969b7b69948eb3dc4db59119591 to your computer and use it in GitHub Desktop.
Save jianminchen/ff048969b7b69948eb3dc4db59119591 to your computer and use it in GitHub Desktop.
HackerRank world code sprint #6 - BrontTrousle - try to shorten the time - failed, still score 0/50, only pass 1 test case
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace boneTrousle
{
public class Node
{
public string purchaseBoxes;
public int length;
public int boxCount;
public long sum;
}
class Program
{
static void Main(string[] args)
{
int queries = Convert.ToInt32(Console.ReadLine().Trim());
for (int i = 0; i < queries; i++)
{
string[] arr = Console.ReadLine().Split(' ');
long total = Convert.ToInt64(arr[0]);
long storeKBox = Convert.ToInt64(arr[1]);
int purchaseBoxCount = Convert.ToInt32(arr[2]);
IList<string> list = new List<string>();
buildResult_usingDFS_stack(total, storeKBox, purchaseBoxCount, list); // think about design here
string res = "-1";
if (list.Count > 0)
{
string[] arr2 = list[0].Split(';');
res = "";
bool skipFirst = true;
foreach (string s in arr2)
{
if (skipFirst)
{
res += s;
skipFirst = false;
}
else
{
res += " " +s;
}
}
}
Console.WriteLine(res);
}
}
/*
* source code reference:
* https://gist.github.com/jianminchen/872bf70039fa8c61ff208b34a591c8ec
*
*/
public static void buildResult_usingDFS_stack(long total, long storeKBox, int purchaseBoxCount, IList<string> list)
{
// need to complete the task using Queue
if (purchaseBoxCount == 0 || storeKBox == 0)
return;
if(purchaseBoxCount > storeKBox/2)
{
long rest = sum(storeKBox);
if (total > rest)
return;
rest -= total;
buildResult_usingDFS_stack(rest, storeKBox, (int)(storeKBox - purchaseBoxCount), list);
return;
}
Stack<Node> stack = new Stack<Node>();
int[] arr = new int[2] { 0, 1 };
foreach (int val in arr)
{
Node node = new Node();
node.length = 1;
if (val != 0)
{
node.purchaseBoxes = val.ToString();
node.boxCount = 1;
node.sum = 1;
}
else
{
node.purchaseBoxes = "";
node.boxCount = 0;
node.sum = 0;
}
stack.Push(node);
}
while (stack.Count > 0)
{
Node node = (Node)stack.Pop();
string s1 = node.purchaseBoxes;
int len = node.length;
int boxCount = node.boxCount;
long sum = node.sum;
if (boxCount == purchaseBoxCount)
{
if (sum == total)
{
list.Add(s1);
return;
}
}
else if(len + 1 <= storeKBox)
{
int[] tmpArr = new int[2] { 0, len + 1 };
foreach (int val in tmpArr)
{
Node newNode = new Node();
newNode.purchaseBoxes = s1 + (val==0?"":(";"+val.ToString())) ;
newNode.length = len +1;
newNode.boxCount = val==0? boxCount : (boxCount +1);
newNode.sum = val ==0? sum : (sum + val);
stack.Push(newNode);
}
}
}
return;
}
public static long sum(long n)
{
return (long)(n * (n + 1) / 2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment