Jump Game II leetcode 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.
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.)
题解:
参考了http://blog.csdn.net/linhuanmars/article/details/21356187,这道题和Jump Game都是利用动态规划的思想。区别是,上一道题维护的全局最优是maxcover,一旦maxcover大于总长度,那么说明能跳到结尾。
而这道题除了维护maxcover外,还需要考虑维护最小步数,最小步数的维护靠maxcover作为每一步能跳的长度,代码如下:
1 public int jump(int[] A) {
2 if(A==null||A.length==0)
3 return 0;
4
5 int maxcover = 0;
6 int step = 0;
7 int lastcover = 0;
8 for(int i = 0; i<=maxcover&&i<A.length;i++){
9 if(i>lastcover){
step++;
lastcover = maxcover;
}
if(A[i]+i>maxcover)
maxcover = A[i]+i;
}
if(maxcover<A.length-1)
return 0;
return step;
}
Jump Game II leetcode java的更多相关文章
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
- Word Break II leetcode java
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- Palindrome Partitioning II Leetcode java
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- Remove Duplicates from Sorted List II leetcode java
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- Permutations II leetcode java
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- Ugly Number II leetcode java
问题描述: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fa ...
- Word Ladder II leetcode java
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- Binary Tree Level Order Traversal II leetcode java
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
随机推荐
- [leetcode greedy]134. Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- Swift2.0语言教程之函数的返回值与函数类型
Swift2.0语言教程之函数的返回值与函数类型 Swift2.0中函数的返回值 根据是否具有返回值,函数可以分为无返回值函数和有返回值函数.以下将会对这两种函数类型进行讲解. Swift2.0中具有 ...
- leetcode x 的平方根 python
x 的平方根 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: ...
- 时间插件datetimepicker
相关datetimepicker用法查看官网http://eonasdan.github.io/bootstrap-datetimepicker/ {% load staticfiles %} < ...
- hdu 1429 bfs+状压
题意:这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开始 Ignatius被关在(sx,sy)的位置,离开地牢的门 ...
- FLASK开发简易blog的学习笔记
首先,学会了如何创建一个新的包.就是在文件夹下创建__init__.py文件,就是在其他地方导入这个包了.
- opencv第二课 进行缩放图片~
#include<stdio.h> #include<iostream> #include<opencv2\opencv.hpp> using namespace ...
- Topcoder SRM 643 Div1 250<peter_pan>
Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...
- visio2013 激活工具,仅供交流学习
visio2013激活软件 环境是 win7, 64 bit 装了 visio 2013 , 可以却不能用它来画图,在网上找了一些破解工具,大都不能解决问题.网上不靠谱的广告型文章太多了 所幸,终于找 ...
- spring---transaction(1)---源代码分析(事务的拦截器TransactionInterceptor)
写在前面: 先了解一下spring的事务.分为分明式事务管理和注解式事务管理,对于前期的事务,spring会通过扫描拦截对于事务的方法进行增强(以后讲解). 若果目标方法存在事务,spring产出的b ...