You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/google/Level4/Challenge1/README.md
+81-11Lines changed: 81 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ You and your rescued bunny prisoners need to get out of this collapsing death tr
7
7
8
8
The**time it takes to move** from your starting point to all of the bunnies and to the bulkhead will be given to you in a square matrix of integers.**Each row will tell you the time it takes to get to the start, first bunny, second bunny, ..., last bunny, and the bulkhead in that order**. The order of the rows follows the same pattern (start, each bunny, bulkhead). The bunnies can jump into your arms, so picking them up is instantaneous, and arriving at the bulkhead at the same time as it seals still allows for a successful, if dramatic, escape. (Don't worry, any bunnies you don't pick up will be able to escape with you since they no longer have to carry the ones you did pick up.) You can revisit different spots if you wish, and moving to the bulkhead doesn't mean you have to immediately leave -**you can move to and from the bulkhead to pick up additional bunnies if time permits**.
9
9
10
-
In addition to spending time traveling between bunnies, some paths interact with the space station's security checkpoints and add time back to the clock. Adding time to the clock will delay the closing of the bulkhead doors, and if the time goes back up to 0 or a positive number after the doors have already closed, it triggers the bulkhead to reopen. Therefore, it might be possible to walk in a circle and keep gaining time: that is**each time a path is traversed, the same amount of time is used or added**.
10
+
In addition to spending time traveling between bunnies, some paths interact with the space station's security checkpoints and**add time back to the clock**. Adding time to the clock will delay the closing of the bulkhead doors, and if the time goes back up to 0 or a positive number after the doors have already closed, it**triggers the bulkhead to reopen**. Therefore, it might be possible to walk in a circle and keep gaining time: that is**each time a path is traversed, the same amount of time is used or added**.
11
11
12
12
Write a function of the form solution(times, time_limit) to calculate the most bunnies you can pick up and which bunnies they are, while still escaping through the bulkhead before the doors close for good. If there are multiple sets of bunnies of the same size,**return the set of bunnies with the lowest prisoner IDs (as indexes) in sorted order**. The bunnies are represented as a sorted list by prisoner ID, with the**first bunny being 0**.There are at most**5 bunnies**, and time_limit is a non-negative integer that is at most***999**.
13
13
@@ -70,7 +70,7 @@ Use verify [file] to test your solution and see how it does. When you are finish
70
70
71
71
##First Analysis
72
72
73
-
###Provided Case 1
73
+
###Provided Case 1: Time Added Back
74
74
Time Limit: 1
75
75
Answer: Bunnies 1, 2
76
76
@@ -84,21 +84,22 @@ Answer: Bunnies 1, 2
84
84
Start End Delta Time Status
85
85
- 0 - 1 // Bulkhead initially open
86
86
87
-
0 4 -1 2 // Add time. As the minimal time needed is 2 to rescuse any bunny,it is optedto add more time. And to add time we can jumptoBulkhead once.
87
+
0 4 -1 2 // Add time. As the minimal time needed is 2 to rescuse any bunny,we needto add1more time which can be gained by opening Bulkhead as 0to4 is -1.
88
88
89
-
4 2 2 0 // Rescue a bunny. As there are bunnies those requires time which we have gathered, i.e. 2, it is opted to rescue Bunny 1.
89
+
4 2 2 0 // Rescue a bunny. As there are bunnies those requires time which we have gathered, i.e. 2, it is opted to rescue Bunny 1.
90
90
91
-
2 4 -1 1 // Add time.Now rescue more bunny we needtoget more time so weneed togo throughbulkhead again.
91
+
2 4 -1 1 // Add time.Although we can exit at this point but goingtoBulhead add timebackso we go throughit.
92
92
93
-
4 3 2 -1 //Go to secondbunny as the time taken is 2,we will have to 1 timetogive back. We can not go to first bunny as it takes 3 to carry bunny and we will not be able to reopen the bulkhead.
93
+
4 3 2 -1 //Future calculation. Rescue Secondbunny as the time taken is 2,and from there is pathtoBulkhead which adds back time while going through.
94
94
95
-
3 4 -1 0 Open bulkhead. Exit. As reopening bulkhead add 1, we can return the time we lended, i.e. -1.
95
+
3 4 -1 0 // Exit. The last Third bunny is left to rescue but as the time is 0 we will not be able to rescue it.
96
+
96
97
97
98
>What if I try to recuse all of the bunnies?
98
99
99
100
100
101
101
-
###Provided Case 2
102
+
###Provided Case 2: No Time Added Back
102
103
103
104
Time Limit: 3
104
105
Answer: Bunnies 0, 1
@@ -112,9 +113,9 @@ Answer: Bunnies 0, 1
112
113
113
114
Start End Delta Time Status
114
115
- 0 - 3 Bulkhead is open
115
-
0 1 2 2 Rescuebunny one
116
-
1 2 1 1 Rescuebunny two
117
-
2 4 0 0 Exit. We can go directly to 4 as it takes 1 time to open the bulkhead.
116
+
0 1 2 2 RescueFirst Bunny.
117
+
1 2 1 1 RescueSecond Bunny.
118
+
2 4 0 0 Exit.
118
119
119
120
If we wanted to rescue third bunny then we needed to add more time, which is slows the process, so we skip them as the requirement to the problem was to be fast! But there could have been another solution to rescue 0,3 0r 1,3 bunny as well as every rescure needs exactly same time.
120
121
@@ -420,3 +421,72 @@ From the research solution could be found in the following steps:
420
421
-[ ] Understand and implement the finding number 3.
Went back to the problem and update the description to the given examples above.
428
+
429
+
####Some Theory
430
+
431
+
>Graph representations
432
+
Adjacency Matrix is a n-by-n matrix with A uv = 1 if (u, v) is an edge.
433
+
- Two representations of each edge.
434
+
- Space proportional to n 2 .
435
+
- Checking if (u, v) is an edge takes Θ(1) time.
436
+
- Identifying all edges takes Θ(n 2 ) time.
437
+
438
+
Adjacency Lists is a node-indexed array of lists.
439
+
- Two representations of each edge.
440
+
- Space is Θ(m + n).
441
+
- Checking if (u, v) is an edge takes O(degree(u)) time. degree = number of neighbors of u.
442
+
- Identifying all edges takes Θ(m + n) time.
443
+
444
+
445
+
Cycles: A cycle is a path v 1 , v 2 , ..., v k in which v 1 = v k and k ≥ 2.
446
+
Trees: An undirected graph is a tree if it is connected and does not contain a cycle.
447
+
448
+
Shortest path problem. Given two nodes s and t, what is the length of a shortest path between s and t?
449
+
450
+
**In our problem we intend to return the nodes which can be reached with the time we have.**
451
+
452
+
Breadth-first search: Explore outward from s in all possible directions, adding nodes one “layer” L at a time.
453
+
BFS algorithm.
454
+
- L0 = {s}
455
+
- L1 = all neighbors of L0
456
+
- L2 = all nodes that do not belong to L0 or L1 , and that have an edge to a node in L1
457
+
- Li+1 = all nodes that do not belong to an earlier layer, and that have an edge to a node in Li
458
+
459
+
BFS runs in O(m + n) time if the graph is given by an adjacency representation.
460
+
461
+
Connected component. Find all nodes reachable from a point.
462
+
463
+
**This kind of represents our problem** - Which nodes can can we reach from start with the limited time constraints.
464
+
465
+
>Graph search
466
+
- Directed reachability. Given a node s, find all nodes reachable from s.
467
+
- Directed s↝t shortest path problem. Given two nodes s and t, what is the length of a shortest path from s to t?
468
+
- Graph search. BFS extends naturally to directed graphs.
469
+
470
+
>Strong connectivity
471
+
- Def. Nodes u and v are mutually reachable if there is both a path from u to v and also a path from v to u.
472
+
- Def. A graph is strongly connected if every pair of nodes is mutually reachable.
473
+
474
+
>Directed acyclic graphs
475
+
- Def. A DAG is a directed graph that contains no directed cycles.
476
+
- Def. A topological order of a directed graph G = (V, E) is an ordering of its nodes as v 1 , v 2 , ..., v n so that for every edge (v i , v j ) we have i < j.
477
+
478
+
>Cycles
479
+
Def. A path is a sequence of edges which connects a sequence of nodes.
480
+
Def. A cycle is a path with no repeated nodes or edges other than the starting and ending nodes.
481
+
482
+
>Minimum spanning tree (MST)
483
+
Def. Given a connected, undirected graph G = (V, E) with edge costs ce, a minimum spanning tree (V, T ) is a spanning tree of G such that the sum of the edge costs in T is minimized.
484
+
485
+
>Arborescence
486
+
In graph theory, an arborescence is a directed graph in which, for a vertex u called the root and any other vertex v, there is exactly one directed path from u to v. An arborescence is thus the directed-graph form of a rooted tree, understood here as an undirected graph. Equivalently, an arborescence is a directed, rooted tree in which all edges point away from the root; a number of other equivalent characterizations exist. Every arborescence is a directed acyclic graph, but not every DAG is an arborescence. An arborescence can equivalently be defined as a rooted digraph in which the path from the root to any other vertex is unique.
487
+
488
+
>Algorithmic Paradigms:
489
+
- Greed: Process the input in some order, myopically making irrevocable decisions.
490
+
- Divide-and-conquer: Break up a problem into independent subproblems; Solve each subproblem; Combine solutions to subproblems to form solutionto original problem.
491
+
- Dynamic programming: Break up a problem into a series of overlapping subproblems; Combine solutions to smaller subproblems to form solution to large subproblem.Dynamic programming is a fancy name for caching intermediate results in a table for later reuse.