45. Jump Game II (JAVA)
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.
Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: 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.
Note:
You can assume that you can always reach the last index.
贪心算法,每次step都是选择最远的那个。
class Solution {
public int jump(int[] nums) {
if(nums.length == 1) return 0; //no need steps int start = 1; //start index of current step
int end = nums[0]; //end index of current step
int step = 1; //minimum steps
int maxStep = 0; //furthest index of current step
while(end < nums.length-1){
for(int i = start; i <= end; i++){
maxStep = Math.max(maxStep, nums[i]+i);
}
start = end+1;
end = maxStep;
step++;
}
return step;
}
}
45. Jump Game II (JAVA)的更多相关文章
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- Leetcode 55. Jump Game & 45. Jump Game II
55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- [leetcode] 45. 跳跃游戏 II(Java)(动态规划)
45. 跳跃游戏 II 动态规划 此题可以倒着想. 看示例: [2,3,1,1,4] 我们从后往前推,对于第4个数1,跳一次 对于第3个数1,显然只能跳到第4个数上,那么从第3个数开始跳到最后需要两次 ...
- 55 Jump Game i && 45 Jump Game ii
Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...
- 【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 (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] 45. Jump Game II 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- C# Socket-TCP异步编程原理详解附源码
目录 目录异步原理主要方法源码Server源码:Client源码实验效果(广播为例)参考博客 TOC 异步原理 套接字编程原理:延续文件作用思想,打开-读写-关闭的模式. C/S编程模式如下: Ø 服 ...
- js 放回上一页
window.history.go(-1);//返回上一页不刷新 window.location.href = document.referrer;//返回上一页并刷新
- mysql修改数据表某列的配置
alter table 表名 modify column 字段名 类型;alter table 表名 drop column 字段名
- React Native商城项目实战05 - 设置首页的导航条
1.Home.js /** * 首页 */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Te ...
- linux中 > 、>> 的用法
linux中>表示覆盖原文件内容(文件的日期也会自动更新),>>表示追加内容(会另起一行,文件的日期也会自动更新). 1 将history命令执行的结果保存到history.log文 ...
- 搜索练习题——FBI树
目录: ·题目描述 ·知识拓展 ·题目分析 ·思路分析 ·代码实现 ·总结 ·题目描述: (洛谷P1087 FBI树) 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称 ...
- 无法加载程序集XXX.dll 此程序集可能是从 Web 上下载的
错误 13 无法加载程序集 file:///D:\Documents\Downloads\kaxaml-master\kaxaml-master\packages\Prism.4.0.0. ...
- html不点击提交,自动post
<!DOCTYPE html> <html> <form id="form1" name="form1" method=" ...
- Linux安装MySql5.7及配置(yum安装)
Linux安装MySql5.7及配置(yum安装) [root@xld ~]# rpm -q centos-release centos-release-7-7.1908.0.el7.centos.x ...
- HTML5——新表单元素 表单属性 语义元素
表单元素 新的表单元素 datalist——输入域选项列表 keygen——提供一种验证用户的可靠方法 output——不同类型的输出,比如计算或脚本输出 表单属性 <form> / &l ...