-
Notifications
You must be signed in to change notification settings - Fork 28
/
Frog_Jump.cpp
41 lines (40 loc) · 1.62 KB
/
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
30
31
32
33
34
35
36
37
38
39
40
41
class Solution {
bool canCross(int indx, int unit, vector<int>& stones, unordered_map<int, int>& stoneMap, vector<vector<bool>>& visited, vector<vector<bool>>& dp) {
if(indx == stones.size() - 1) {
return true;
}
if(indx >= stones.size()) {
return false;
}
if(visited[indx][unit]) {
return dp[indx][unit];
}
bool isReached = false;
if(stoneMap[stones[indx] + unit + 1]) {
int nextIndx = stoneMap[stones[indx] + unit + 1] - 1;
isReached |= canCross(nextIndx, unit + 1, stones, stoneMap, visited, dp);
}
if(!isReached and unit > 0 and stoneMap[stones[indx] + unit]) {
int nextIndx = stoneMap[stones[indx] + unit] - 1;
isReached |= canCross(nextIndx, unit, stones, stoneMap, visited, dp);
}
if(!isReached and unit - 1 > 0 and stoneMap[stones[indx] + unit - 1]) {
int nextIndx = stoneMap[stones[indx] + unit - 1] - 1;
isReached |= canCross(nextIndx, unit - 1, stones, stoneMap, visited, dp);
}
visited[indx][unit] = true;
return dp[indx][unit] = isReached;
}
public:
bool canCross(vector<int>& stones) {
int n = stones.size();
const int MAX_UNIT = 100;
vector<vector<bool>> dp(n, vector<bool>(MAX_UNIT, false));
vector<vector<bool>> visited(n, vector<bool>(MAX_UNIT, false));
unordered_map<int, int> stoneMap;
for(int i = 1; i <= n; ++i) {
stoneMap[stones[i - 1]] = i;
}
return canCross(0, 0, stones, stoneMap, visited, dp);
}
};