LeetCode 045 Jump Game II
题目要求: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(int A[], int n) {
int step = 0;
int left = 0;
int right = 0; //用来记录局部点的最大步长
if(n == 1) return 0;
while(left <= right){
step++;
const int old_right = right;
//贪心算法
//局部最优解
for(int i = left; i <= old_right; i++){
//计算每次到达的地方(局部)
int new_right = i + A[i];
//如果new_right超过n-1,则表示到了结尾
if(new_right >= n - 1) return step;
//保证right是最大
if(new_right > right) right = new_right;
}
//左值放在最优解的下一个
left = old_right + 1;
}
return 0;
}
};
LeetCode 045 Jump Game II的更多相关文章
- Java for LeetCode 045 Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 45 Jump Game II(按照数组进行移动)
题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...
- 045 Jump Game II 跳跃游戏 II
给定一个非负整数数组,你最初位于数组的首位.数组中的每个元素表示你在该位置的最大跳跃长度.你的目标是用最小跳跃次数到达最后一个索引.例如: 给定一个数组 A = [2,3,1,1,4]跳到最后一个索引 ...
- [LeetCode] 45. Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- MyBatis 原理浅析——基本原理
前言 MyBatis 是一个被广泛应用的持久化框架.一个简单的使用示例如下所示,先创建会话工厂,然后从会话工厂中打开会话,通过 class 类型和配置生成 Mapper 接口的代理实现,最后使用 Ma ...
- 《Clojure编程》笔记 第1章 进入Clojure仙境
目录 背景简述 第1章 进入Clojure仙境 1.1 基础概念 1.2 常用的一些符号 背景简述 本人是一个自学一年Java的小菜鸡,理论上跟大多数新手的水平差不多,但我入职的新公司是要求转Cloj ...
- [Luogu P3455] [POI2007]ZAP-Queries (莫比乌斯反演 )
题面 传送门:洛咕 Solution 这题比这题不懂简单到哪里去了 好吧,我们来颓柿子. 为了防止重名,以下所有柿子中的\(x\)既是题目中的\(d\) 为了方便讨论,以下柿子均假设\(b>=a ...
- 微信小程序授权页面
这里也是比较简单的 直接复制粘贴就可以用,可能图片位置不对.. <template> <view id="imporwer"> <image src= ...
- ERP的权限管理的操作与设计--开源软件诞生24
赤龙ERP用户与权限管理讲解--第24篇 用日志记录"开源软件"的诞生 [进入地址 点亮星星]----祈盼着一个鼓励 博主开源地址: 码云:https://gitee.com/re ...
- 纯css实现箭头
很久之前收集的,忘记出处了. 1.梯形: 当元素宽.高和边框的宽相近(等)时,改变某一边的颜色可以看到一个梯形: border: 10px solid #000; border-left-color: ...
- tensorflow-gpu2.1缺少libcudnn.so.7
下载CUDA对应版本的cuDnn. 下载后在cuDnn/cuda/lib64下有libcudnn.so.7这个文件,把它复制到/usr/local/cuda/lib64/下即可
- MySQL视图详细介绍
前言: 在MySQL中,视图可能是我们最常用的数据库对象之一了.那么你知道视图和表的区别吗?你知道创建及使用视图要注意哪些点吗?可能很多人对视图只是一知半解,想详细了解视图的同学看过来哟,本篇文章会详 ...
- Ubuntu18.04上安装CUDA_10.1(nvidia-driver)和cuDNN_7.6.5
本文是在Ubuntu18.04.5服务器上安装CUDA_10.1(nvidia-driver455)和cuDNN_7.6.5, Ubuntu 18.04.5 CUDA_10.1 (nvidia-dri ...
- redis-server文件启动cmd一闪而过
工作上需要在本地装redis,所以就帮别人排查了一个问题,就是redis服务双击了之后不能起来,就是一个黑色的cmd框一闪而过,正常的是这样的: 然而,我当时第一次接触windows上的redis服务 ...