forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-sideway-jumps.cpp
48 lines (45 loc) · 1.19 KB
/
minimum-sideway-jumps.cpp
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
// Time: O(n)
// Space: O(1)
// greedy solution
class Solution {
public:
int minSideJumps(vector<int>& obstacles) {
int result = 0;
unordered_set<int> lanes = {2};
for (int i = 0; i + 1 < size(obstacles); ++i) {
lanes.erase(obstacles[i + 1]);
if (!empty(lanes)) {
continue;
}
++result;
for (int j = 1; j <= 3; ++j) {
if (j != obstacles[i] && j != obstacles[i + 1]) {
lanes.emplace(j);
}
}
}
return result;
}
};
// Time: O(n)
// Space: O(1)
// dp solution
class Solution2 {
public:
int minSideJumps(vector<int>& obstacles) {
static const int MAX_N = 5 * 1e5;
static const int INF = (MAX_N - 1) + 1;
array<int, 3> dp{1, 0, 1};
for (const auto& i : obstacles) {
if (i) {
dp[i - 1] = INF;
}
for (int j = 0; j < 3; ++j) {
if (j + 1 != i) {
dp[j] = min({dp[0] + (j != 0), dp[1] + (j != 1), dp[2] + (j != 2)});
}
}
}
return *min_element(cbegin(dp), cend(dp));
}
};