-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrog_Jump.cpp
More file actions
26 lines (25 loc) · 843 Bytes
/
Copy pathFrog_Jump.cpp
File metadata and controls
26 lines (25 loc) · 843 Bytes
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
class Solution {
public:
bool canCross(vector<int>& stones) {
for(int i=3; i<stones.size(); i++)
if(stones[i]>stones[i-1]*2)
return false;
unordered_set<int> ust(stones.begin(),stones.end());
stack<pair<int,int>> dfs;
dfs.push({0,0});
int last_position = stones[stones.size()-1];
while(!dfs.empty()){
auto curr = dfs.top();dfs.pop();
for(int k=curr.second-1; k<=curr.second+1; k++){
if(k<=0)
continue;
int next_position = curr.first + k;
if(next_position == last_position)
return true;
if(ust.count(next_position))
dfs.push({next_position,k});
}
}
return false;
}
};