forked from cmhungsteve/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frog-jump.cpp
29 lines (25 loc) · 834 Bytes
/
frog-jump.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
// Time: O(n) ~ O(n^2)
// Space: O(n)
class Solution {
public:
bool canCross(vector<int>& stones) {
if (stones[1] != 1) {
return false;
}
unordered_map<int, unordered_set<int>> last_jump_units;
for (const auto& s: stones) {
last_jump_units.emplace(s, {unordered_set<int>()});
}
last_jump_units[1].emplace(1);
for (int i = 0; i + 1 < stones.size(); ++i) {
for (const auto& j : last_jump_units[stones[i]]) {
for (const auto& k : {j - 1, j, j + 1}) {
if (k > 0 && last_jump_units.count(stones[i] + k)) {
last_jump_units[stones[i] + k].emplace(k);
}
}
}
}
return !last_jump_units[stones.back()].empty();
}
};