leetcode https://oj.leetcode.com/problems/jump-game-ii/
1.超时的,效率太低
public class Solution {
public int jump(int[] A) {
int len=A.length;
int d[]=new int[len];
d[0]=0;
for(int i=1;i<len;i++)
{
d[i]=1;
int j;
for(j=1;j+i<len&&j<=A[j];j++)
{
d[i+j]=Math.max(d[i]+1,d[j+i]);
}
if(j+i==len) return d[len-1];
}
return d[len-1];
}
}
其实根本不用判断的的,只要求出覆盖区域一定是当前最长+1;
public class Solution {
public int jump(int[] A) {
int len=A.length;
int step=0;
int last=0;
int maxpost=0;
for(int i=0;i<len;i++)
{
if(i>last)
{
step++;
last=maxpost;
}
maxpost=Math.max(i+A[i],maxpost);
}
return step;
}
}
AC代码
public class Solution {
public int jump(int[] A) {
int len=A.length; int step=0;//记录当前的步骤
int last=0; //当前步骤达到的最大值
int maxpost=0;//当前能扩张的最大范围 ,很像bfs啊,一层一层的,bfs就是求最小距离的啊 for(int i=0;i<len;i++)
{
if(i>last)
{
step++;
last=maxpost; } maxpost=Math.max(i+A[i],maxpost); } return step; }
}
leetcode https://oj.leetcode.com/problems/jump-game-ii/的更多相关文章
- https://oj.leetcode.com/problems/majority-element/
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode shttps://oj.leetcode.com/problems/surrounded-regions/
1.从外围搜索O,深度搜索出现了 Line 35: java.lang.StackOverflowError Last executed input: ["OOOOOOOOOOOOOOOOO ...
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode——Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [leetcode]Best Time to Buy and Sell Stock II @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意: Say you have an array ...
- [leetcode]Populating Next Right Pointers in Each Node II @ Python
原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...
- LeetCode OJ-- Populating Next Right Pointers in Each Node II **@
https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 接上一题目,输入的树不是perfect ...
- 【原创】leetCodeOj --- Jump Game II 解题报告
原题地址: https://oj.leetcode.com/problems/jump-game-ii/ 题目内容: Given an array of non-negative integers, ...
随机推荐
- Android混淆那些事儿
博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 博客园:追风917 # Android混淆 Android混淆是Android开发者经常使用的一种用于代码防止被反编译的 ...
- java csv - 读写及其操作.
今天帮同学处理数据, 主要是从1w多条记录中随机获取8k条, 然后再从8k条记录中随机获取2k条记录. 最后将2k条记录中随机分成10组,使得每组的记录都不重复. 下面将我的代码都贴上来, 好以后处理 ...
- 如何在OpenWRT环境下做开发
1.搭建开发环境 首先,在执行make menuconfig后,会出现下图: 其中,图中红框部分是我定制路由器的系统版本,大家可以根据不同的路由器进行不同的选择:绿框部分表示我们需要编译一个SDK开发 ...
- EL标签和JSTL标签---JSP页面的应用
====EL(Expression Language)表达式语言:用于计算和输出存储在标志位置(page.request.session.application)的java对象的值: 1.开启和关闭E ...
- Köln-keith jarrett
在火车上遇到一男生,带着他弟弟.他弟弟跑来跑去的,他就安稳地坐在下铺看书,不知道是哪种语言. 我们都是在北京下车. 第二天在王府井吃饭,姐姐带我吃西餐.我又看到他,跟一个阿姨一起吃饭. 吃饭的时候姐姐 ...
- [LeetCode OJ] Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- IE9透明filter和opacity同时生效的解决办法 IE9 hack only
转载请注明:来自于http://www.cnblogs.com/bluers/ 问题: 假设结构如下: <div class="wrapper"> <p clas ...
- R.Net 用法
using (REngine engine = REngine.CreateInstance("RDotNet")) { engine.Initialize(); var expa ...
- php和js根据子网掩码和ip计算子网
php $ip = '192.168.6.1'; $mask = '255.255.2.0'; $sub_net = array();//子网 $ip_explode = explode('.', $ ...
- Docker安装Gitlab
一.Ubuntu16.4上Docker安装Gitlab 1.安装docker 参见:https://docs.docker.com/engine/installation/linux/ubuntuli ...