Jump Game II
2015-07-02 01:12
169 查看
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
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(vector<int>& nums) { int result = 0; int cur = 0; int n = nums.size(); if (n == 1) { return 0; } while (n-1 > cur+nums[cur]) { int maxLen = 0; int next = cur; for (int i = cur+1; i <= cur+nums[cur]; i++) { //贪心算法 if (i-cur+nums[i] > maxLen) { maxLen = i-cur+nums[i]; next = i; } } cur = next; result++; } result++; return result; } };
相关文章推荐
- 关于Linux系统调用,内核函数
- 文件测试运算符列表
- 2015.07.01总结
- 编译型/解释型语言,什么时候用shell
- 二分图最大匹配,最小路径覆盖,最小点覆盖,最大独立集,最小边覆盖与建图方法
- Vijava 学习笔记之(Cluster 关联资源信息:CPU、内存、Datastore容量信息)
- 编译型/解释型语言,什么时候用shell
- rsync
- HDFS详细分析二
- Flume原理、安装和使用
- POJ 1149 PIGS ( 网络流 )
- Lua 脚本开发学习
- MySQL服务器管理
- java 半角全角互相转换
- Vijava 学习笔记之(虚拟机可扩容CPU核数、内存大小和关联DataStore)
- 解决TextView排版自动换行问题
- Shell定时器
- 一个关于数据类型的问题
- Linux下获取本机IP地址
- C语言 基础60题(2)――二维数组操作