-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfrogjumps.cpp
More file actions
38 lines (38 loc) · 1.05 KB
/
Copy pathfrogjumps.cpp
File metadata and controls
38 lines (38 loc) · 1.05 KB
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
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;
}
set<int>st;
for(int i=0;i<stones.size();i++)
st.insert(stones[i]);
stack<int>positions,jumps;
positions.push(0);
jumps.push(0);
int laststone=stones[stones.size()-1];
while(!positions.empty())
{
int pos=positions.top();
positions.pop();
int jumpPositions=jumps.top();
jumps.pop();
for(int i=jumpPositions-1;i<=jumpPositions+1;i++)
{
if(i<=0)
continue;
int nextposition=pos+i;
if( nextposition==laststone)
return true;
else if(st.find(nextposition)!=st.end())
{
positions.push(nextposition);
jumps.push(i);
}
}
}
return false;
}
};