-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLimited Subway Surfer
More file actions
57 lines (54 loc) · 2.42 KB
/
Limited Subway Surfer
File metadata and controls
57 lines (54 loc) · 2.42 KB
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
Limited Subway Surfer
Nidhi has created an alternate version of Subway Surfer game. Her new version is not on a train track unlimited length, rather limited to a track of length N + 1. Just like the original game, this alternate version also has three lanes.
The track has points on it ranging from 0 to N.
In the three lanes, at some points, certain manholes are present. Now, to avoid these manholes, our player needs to jump sideways and switch lanes, as he cannot go from i to (i + 1)th point on the same lane if there is no manhole on the lane at point i + 1.
You are given an array manholes of length N + 1 where manholes[i] tells us on which lane is the manhole present for point i. For example, if manholes[2] = 1, this means that on point 2 of the train track, a manhole is present on lane 1.
Find the minimum number of lane changes our player has to perform in order to reach from point 0 to point N on the train track.
Note:
If manholes[i] = 0, this means that for point i, there is no manhole present on any lane.
manholes[0] and manholes[N] are always 0.
Input Format:
First line of input contains integer T, representing the number of test cases.
First line of each test case contains the integer N.
Second line of each test case contains N + 1 space separated integers, representing manholes[i].
Our player always begins from the middle lane.
Constraints:
manholes.length == n + 1
1 <= t <= 100
1 <= N <= 5 * 10^4
0 <= manholes[i] <= 3
manholes[0] == manholes[n] == 0
Output Format:
Minimum number of lane changes to reach from point 0 to N.
Sample Input:
4
0 1 2 3 0
Sample Output:
2
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class Solution {
static public int minLaneChanges(int[] manholes) {
// Write your code here
int N = manholes.length;
return minChangeInLane(manholes,N);
}
public static int minChangeInLane(int barrier[], int n)
{
int dp[] = { 1, 0, 1 };
for (int j = 0; j < n; j++) {
int val = barrier[j];
if (val > 0) {
dp[val - 1] = (int) 1e6;
}
for (int i = 0; i < 3; i++) {
if (val != i + 1) {
dp[i] = Math.min(dp[i],
Math.min(dp[(i + 1) % 3],
dp[(i + 2) % 3])
+ 1);
}
}
}
return Math.min(dp[0], Math.min(dp[1], dp[2]));
}
}