45. Jump Game II
来源:互联网 发布:c语言utf8转unicode 编辑:程序博客网 时间:2024/06/06 04:17
题目:
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.)
本题的思路是基于贪心算法,对于每一个(curStart,curEnd)区间都有一个最远距离 dist
每当我们到达一个最远距离的时候,更新(curStart,curEnd)即可
注意边界条件是nums.size()-1
class Solution {public: int jump(vector<int>& nums) { int step=0,curEnd=0,dist=0; for(int i=0;i<nums.size()-1;i++){ dist=max(dist,nums[i]+i); if(i==curEnd){ step++; curEnd=dist; } } return step; }};
阅读全文
0 0
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- c语言 linux下文件i/o 实现文件的复制
- STM32F107——OTG模块之USB设备之虚拟串口移植(一)
- filter()函数 map()函数
- 函数
- Nodejs 和npm 的关系
- 45. Jump Game II
- jquery的ajax请求data参数
- php里面CGI(common gateway interface),fast-cgi,php-fpm
- Linux命令分析: chgrp
- CodeForces
- MUI 代码键总结
- html框架综合练习(frameset)
- 安卓打印加载等待对的实现
- LeetCode