forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0045.cpp
More file actions
31 lines (29 loc) · 659 Bytes
/
0045.cpp
File metadata and controls
31 lines (29 loc) · 659 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
27
28
29
30
31
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int jump(vector<int>& nums)
{
int pre = -1, cur = 0, step = 0;
for (int i = 0; i < nums.size(); i++)
{
if (cur >= nums.size() - 1) return step;
if (pre < i && i <= cur)
{
step++, pre = cur;
}
cur = max(cur, i + nums[i]);
}
return step;
}
};
int main()
{
vector<int> nums = {3,4,-1,1};
cout << Solution().jump(nums);
return 0;
}