-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path403-frog-jump.cpp
65 lines (61 loc) · 1.66 KB
/
403-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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution {
public:
bool canCross(vector<int>& stones) {
int len = stones.size();
if (len == 0)
{
return false;
}
if (len == 1)
{
return true;
}
if (stones[1] != 1)
{
return false;
}
vector<pair<int, int>> last;
last.push_back(make_pair(1, 1));
for (int i = 2; i < len - 1; i++)
{
int _len = last.size();
if (_len == 0)
{
return false;
}
int j = 0;
int pre = -1;
while (j < _len)
{
if (last[j].first + last[j].second + 1 < stones[i])
{
last.erase(last.begin() + j);
_len--;
continue;
}
if (last[j].first + last[j].second - 1 <= stones[i] && last[j].first + last[j].second + 1 >= stones[i])
{
if (pre != last[j].first)
{
last.push_back(make_pair(stones[i], stones[i] - last[j].first));
pre = last[j].first;
}
}
j++;
}
}
int _len = last.size();
if (_len == 0)
{
return false;
}
for (int j = 0; j < _len; j++)
{
if (last[j].first + last[j].second - 1 <= stones[len - 1] && last[j].first + last[j].second + 1 >= stones[len - 1])
{
return true;
}
}
return false;
}
};