[LeetCode] Jump Game II
2014-10-23 19:07
330 查看
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A =
The minimum number of jumps to reach the last index is
from index 0 to 1, then
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A =
[2,3,1,1,4]
The minimum number of jumps to reach the last index is
2. (Jump
1step
from index 0 to 1, then
3steps to the last index.)
class Solution { public: int jump(int A[], int n) { int count = 0, last = 0, nextmax = 0; for(int i = 0; i < n; i++) { if(last < i) { count++; last = nextmax; } nextmax = max(nextmax, A[i] + i); } return count; } };
相关文章推荐
- leetcode 045 —— Jump Game II
- [leetcode] Jump Game II
- LeetCode OJ Jump Game II
- leetcode 45: Jump Game II
- LeetCode:Jump Game II
- 【LeetCode】Jump Game II
- [leetcode]Jump Game II
- 136_leetcode_Jump Game II
- 【LeetCode】Jump Game II
- [LeetCode39]Jump Game II
- 【LeetCode】Jump Game II 解题报告
- 【LeetCode】Jump Game II
- [leetcode]Jump Game II
- leetcode-Jump Game II (2014.3.3)
- LeetCodee 108 Jump Game II
- [leetcode]Jump Game II
- [leetcode]Jump Game II
- [leetcode解题记录]Jump Game和Jump Game II
- LeetCode Jump Game II(贪心)
- LeetCode Jump Game II