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 ...
随机推荐
- 使用apache的ab命令进行压测
1. 背景:互联网发达的今天,大大小小的网站如雨后春笋,不断出现,但是想要做出一个网站很简单,但是想要做好一个网站,非常非常难,首先:网站做好之后的功能怎么样这都是次要的,主要的是你的网站能承受怎么样 ...
- 将已有的项目提交到GitHub
1.目的: 将已有的项目提交到GitHub 2.准备工作 2.1 此教程建立在对git有初步的理解上 2.2 此教程之前需准备工作 a.熟悉git的一些基本命令和原理. b.已注册有GitHub账号. ...
- luoguP3507 [POI2010]GRA 性质 + 动态规划
题目大意: 给定\(n\)个正整数,\(a, b\)两个人轮流取,\(a\)先手 每次可以取任意多的数,直到取完,每次的得分为取的数中的最小值 \(a, b\)都会使自己的得分减去对手的得分更大,询问 ...
- hdoj 5198 Strange Class 水题
Strange Class Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...
- Linux性能监控分析命令(五)—free命令介绍
性能监控分析的命令包括如下:1.vmstat2.sar3.iostat4.top5.free6.uptime7.netstat8.ps9.strace10.lsof 命令介绍:free命令是监控Lin ...
- Qt线程外使用Sleep
一:方法1 QTime t; t.start(); while(t.elapsed()<1000){ QCoreApplication::processEvents();} 二:方法2 ...
- 实现多线程的另一种方式-Callable
package com.mldn.thread; import java.util.concurrent.ExecutionException; import java.util.concurrent ...
- MySQL DB 主从复制之SSL
需求架构 准备工作 主从服务器时间同步 # 主从服务器同时配置crontab任务,与NTP服务器同步时间即可 */5 * * * * ntpdate 172.16.0.1 &>/dev/ ...
- Java_如何等待子线程执行结束
工作中往往会遇到异步去执行某段逻辑, 然后先处理其他事情, 处理完后再把那段逻辑的处理结果进行汇总的产景, 这时候就需要使用线程了. 一个线程启动之后, 是异步的去执行需要执行的内容的, 不会影响主线 ...
- BZOJ2831(小强的金字塔系列问题--区域整点数求法)
题目:2831: 小强的金字塔 题意就是给出A,B,C,R,L,然后求 这里其实用到扩展欧几里德.(基本上参照clj的解题报告才理解的) 分析:我们先来分析一般情况: 这里我们假设A<C和B&l ...