LeetCode 45 - Jump Game II
2016-03-04 16:00
471 查看
Jump Game II
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
1step
from index 0 to 1, then
3steps to the last index.)
Note:
You can assume that you can always reach the last index.
My Code
class Solution { public: int jump(vector<int>& nums) { int size = nums.size(); vector<int> times(size, 0); for (int i = 0; i < size - 1; i++) for (int j = min(nums[i], size - 1 - i); j > 0; j--) { if (times[i+j] == 0 || times[i+j] > times[i] + 1) times[i+j] = times[i] + 1; else break; } return times.back(); } };Runtime: 16 ms
相关文章推荐
- 支付宝和微信支付封装
- 软件开发模式对比(瀑布、迭代、螺旋、敏捷)
- numpy.reshape
- Android——TV真机调试apk(android:sharedUserId="android.uid.system")转载
- Xamarin.Forms教程下载安装JDK配置环境变量
- 工厂模式详解
- TextView/EditText各种各样的效果
- oracle 11g安装教程 win7 64位
- 数据结构面试题
- $@
- FsRtlIsNameInExpression失败原因
- 98.Which statement is true regarding the COALESCE function?
- Android:30分钟弄明白Touch事件分发机制
- VS2012 项目引用了项目/DLL文件,也写了Using,但是编译时提示:未能找到类型或命名空间名称
- 指针数组与数组指针
- 字符编码问题
- NPOI 导出Excel图片 (网络)
- 微信:实现 pc 端 扫描二维码登陆 的误区
- 长安.ARM体系结构分析
- 水晶报表中使用条码字体