Jump Game II
来源:互联网 发布:python爬虫工程师 编辑:程序博客网 时间:2024/05/16 18:24
题目:
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 = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2
. (Jump 1
step from index 0 to 1, then 3
steps to the last index.)
思想:
本题可以采用贪心算法的思想,每次尽可能多的jump max distance from one region (old-last,last) to anther (last,curr).
代码:
class Solution {public:int jump(vector<int>& nums) {int len = nums.size();int start = 0;//每一跳的起始位置int end = 0;//每一跳的终止位置int jumps = 0;//跳数for (int i = 0; i < len; i++){if (i > end){end = start;jumps++;}start = max(start, i + nums[i]);}return jumps;}};
0 0
- Jump Game &Jump Game II
- Jump Game/Jump Game II
- LeetCode: Jump Game II
- LeetCode Jump Game II
- LeetCode: Jump Game II
- [Leetcode] Jump Game II
- [LeetCode] Jump Game II
- [Leetcode] Jump Game ii
- leetcode Jump Game II
- Jump Game II
- Jump Game II
- Jump Game II
- leetcode Jump Game II
- [LeetCode]Jump Game II
- Jump Game II
- Jump Game II
- Jump Game II
- Jump Game II
- leetcode:LRU Cache
- NOIP2009 Hankson的趣味题
- PHP日志
- 【笔试】12、企业发放的奖金根据利润提成。
- 《算法导论》读书笔记
- Jump Game II
- 标准模板库(STL)使用入门(上)
- hdoj 1513 Palindrome 【LCS】
- MySQL limit用法,分页的实现。
- Android 开发中 iBeacon的使用
- HNU11374:GREAT+SWERC=PORTO(DFS)
- codeforces 522A A. Reposts(树形dp)
- 标准模板库(STL)使用入门(下)
- 数据结构---二叉排序树(BST)