Greedy
来源:互联网 发布:mac窗口关闭快捷键 编辑:程序博客网 时间:2024/05/22 08:18
45. 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 1
step from index 0 to 1, then 3
steps to the last index.)
class Solution {public: int jump(vector<int>& nums) { int n=nums.size(); if(n<=1) return 0; int steps=0; int coverPos=0; for(int i=0;i<n;){ if(nums[i]==0) return -1; coverPos=nums[i]+i; steps++; if(coverPos>=n-1){ return steps; } int maxDistance=0; for(int j=i+1;j<=coverPos;j++){ if(nums[j]+j>maxDistance){ maxDistance=nums[j]+j; i=j; } } } return steps; }};
55. Jump Game
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.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4]
, return true
.
A = [3,2,1,0,4]
, return false
.
class Solution {public: bool canJump(vector<int>& nums) { int n=nums.size(); if(n<=0) return false; int cover=0; for(int i=0;i<=cover && i<n;i++){ if(cover<nums[i]+i){ cover=nums[i]+i; } if(cover>=n-1){ return true; } } return false; }};
- Greedy
- Greedy
- Greedy
- 4221 Greedy?
- Greedy Change
- hdu4221-Greedy?
- greedy algorithm
- Greedy division
- Greedy Mouse
- Greedy Mouse
- uva12325(greedy)
- Greedy algorithm
- hdu4221 Greedy?
- 贪心Greedy
- Greedy? HDU
- LeetCode--Greedy
- hdu4221 Greedy
- hdu4221 Greedy?
- opencv图像resize(改变图片大小)
- Win7x64+cuda8.0+ caffe VS2013配置记录
- MPAndroidChart详解
- autocad 绘制相机模型示意图
- stanford corenlp自定义切词类
- Greedy
- ASN.1概述
- 零基础学习OpenCL(1)-安装OpenCL
- 关于40不惑
- 简单的放大镜效果
- BZOJ2631: tree Link-cut-tree
- 【转】数组、链表、Hash
- 关于socket flush()目标端接收不到数据,close()可以接收到数据的问题
- char