-
Notifications
You must be signed in to change notification settings - Fork 15
/
BreakTheRope.cs
80 lines (62 loc) · 2.16 KB
/
BreakTheRope.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Got 87% without binary search https://codility.com/demo/results/trainingWNABKT-ATU/
using System;
using System.Collections.Generic;
using System.Linq;
class Solution
{
public static void Main(string[] args)
{
// var response = solution(new int[] { 5, 3, 6, 3, 3 }, new int[] { 2, 3, 1, 1, 2 }, new int[] { -1, 0, -1, 0, 3 });
// var response = solution(new int[] { 5 }, new int[] { 2 }, new int[] { -1 });
var response = solution(new int[] { 5 }, new int[] { 6 }, new int[] { -1 });
Console.WriteLine(response);
}
public static int solution(int[] A, int[] B, int[] C)
{
Node roof = new Node() { Id = -1, Durability = int.MaxValue, Weight = 0 };
Node[] nodes = new Node[A.Length]; // node array for O(1) searching
for (int i = 0; i < A.Length; i++)
{
Node newNode = new Node() { Id = i, Weight = B[i], Durability = A[i] - B[i] };
nodes[i] = newNode;
if (newNode.Durability < 0)
{
return i - 1;
}
if (C[i] == -1)
{
roof.Children.Add(newNode);
newNode.Parent = roof;
continue; // if hung from the roof, no need to check
}
else
{
nodes[C[i]].Children.Add(newNode);
newNode.Parent = nodes[C[i]];
}
Node searchNode = nodes[C[i]];
while (searchNode.Id != -1) // go from leaf to roof
{
searchNode.Durability = searchNode.Durability - newNode.Weight;
if (searchNode.Durability < 0) // rope was broken
{
return i;
}
searchNode = searchNode.Parent;
}
}
return A.Length;
}
public class Node
{
public int Weight { get; set; }
public int Id { get; set; }
public int Durability { get; set; }
public Node Parent { get; set; }
public List<Node> Children { get; set; }
public Node()
{
this.Children = new List<Node>();
}
}
}