Created
August 28, 2016 08:03
-
-
Save jianminchen/e3fc7d23a62274b207ddc41a06030cd4 to your computer and use it in GitHub Desktop.
World code sprint #6 - https://www.hackerrank.com/contests/world-codesprint-6/challenges/bonetrousle - pass test case in example, time out/wrong answer, score 0/ 50
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment