-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrogJump.cpp
30 lines (28 loc) · 880 Bytes
/
frogJump.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
// Source: https://leetcode.com/problems/frog-jump/
// Author: Miao Zhang
// Date: 2021-02-08
class Solution {
public:
bool canCross(vector<int>& stones) {
unordered_map<int, bool> dic;
return dfs(stones, 0, 0, dic);
}
private:
bool dfs(vector<int>& stones, int pos, int jump, unordered_map<int, bool>& dic){
int n = stones.size();
int key = pos | jump << 11;
if (pos >= n - 1) return true;
if (dic.count(key)) return dic[key];
for (int i = pos + 1; i < n; i++) {
int dist = stones[i] - stones[pos];
if (dist < jump - 1) continue;
if (dist > jump + 1) return dic[key] = false;
if (dfs(stones, i, dist, dic)) {
dic[key] = true;
return dic[key];
}
}
dic[key] = false;
return dic[key];
}
};