[leetcode] Jump Game II
来源:互联网 发布:淘宝上次认证信息地址 编辑:程序博客网 时间:2024/06/04 21:16
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.)
DP实现,用dp[i]来表示到达i的最小跳数。由于A[i]表示i能跳的最大步长,dp[n]应该是一个递增数组。所以一旦找到一个点j可以更新dp[i]即break。
int jump(int A[], int n) { vector<int> dp(n, 0); for (int i = 1; i < n; ++i) dp[i] = INT_MAX; for (int i = 1; i < n; ++i) { for (int j = 0; j < i; ++j) { if (A[j] + j >= i) { if (dp[j] + 1 < dp[i]) { dp[i] = dp[j] + 1; break; } } } } return dp[n - 1]; }
0 0
- 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
- 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
- LeetCode | Jump Game II
- 【Leetcode】Jump Game II
- LeetCode Jump Game II
- [LeetCode] Jump Game II
- 微信公众帐号开发教程第5篇-各种消息的接收与响应
- MyEclipse中配置struts.xml自动提示
- iOS: 学习笔记, 用代码驱动自动布局实例(swift)
- android RadioButton实现选项卡的效果
- POJ 3278 Catch That Cow
- [leetcode] Jump Game II
- 今天在看PHP视频时,学到了PHP中位运算的使用方式
- Java中的其他类和对象
- 计应121--电子商务论文--李智飞--201236616127
- 股票的基础知识、除权与除息
- 音视频编解码知识
- 【C语言天天练(十二)】命令行参数
- spring1
- Android学习笔记 开发入门(一)——建立第一个应用程序(下)