LeetCode OJ-- Jump Game II **
https://oj.leetcode.com/problems/jump-game-ii/
给一个数列,每次可以跳相应位置上的步数,问跳到最后位置至少用几步。
动态规划:
j[pos]表示从0到pos至少要跳的步数,初始化为n
j[pos] = min { j[i] + 1 ,j[pos]} if(A[i] + i >=pos) i 从0到pos
这属于一维动态规划
class Solution {
public:
int jump(int A[], int n) {
if(n == )
return ; vector<int> ans;
ans.resize(n);
ans[] = ; for(int i = ;i<n;i++)
{
ans[i] = n;//initialize for(int j = ;j<i;j++)
{
if(A[j] + j >= i)
ans[i] = min(ans[i],ans[j] + );
}
}
return ans[n-];
}
};
复杂度为O(n*n)超时了。
于是看了答案,用贪心,类似宽度优先搜索的概念。
维护一个当前范围(left,right)表示从当前范围经过一步可以调到的范围(min_distance,max_distance)。
class Solution {
public:
int jump(int A[], int n) {
if(n == )
return ; if(n==)
return ; int ans_step = ;
int left = , right = ; int max_distance = ;
int min_distance = n;
while()
{
ans_step++;
max_distance = ;
min_distance = n;
int i;
for(i = left; i <= right && i< n;i++)
{
int this_time = i + A[i]; if( this_time > max_distance)
max_distance = this_time; if(max_distance >= n-)
return ans_step;
}
if(i == n)
break; i = left;
bool flag = false;
while(i<=right)
{
//find the first number not 0
if(A[i]!=)
{
min_distance = i + ;
flag = true;
break;
}
i++;
}
if(flag == false)
break;
right = max_distance;
left = min_distance; }
return n;
}
};
LeetCode OJ-- Jump Game II **的更多相关文章
- 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 045 Jump Game II
题目要求:Jump Game II Given an array of non-negative integers, you are initially positioned at the first ...
- [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 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...
- [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
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 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 ...
随机推荐
- 如何提高STM32的学习效率
时间如何安排 做任何事情前,习惯写一个计划——要在一个月内上手STM32! 没有计划的日子,每天早上醒来睁开眼睛,却不知道自己今天要干啥 计划和时间安排: 第一阶段:找感觉——谈及STM32,立即反应 ...
- IOS开发学习笔记038-autolayout 自动布局 界面实现
在storyboard/xib文件中实现自动布局 autolayout 1.注意事项 autolayout和frame属性是有冲突的,所以如果准备使用autolayout,就不要再代码中对控件的fra ...
- Windows命令行中pip install jieba,但没有安装到anaconda3中
系统混淆了python3环境下的pip和anaconda3环境下的pip. 找到Anaconda3的Scripts目录,我这里是C:\Users\Diane\Anaconda3\Scripts 将该目 ...
- sass和postcss
sass是css预处理器 需要安装node-sass支持 核心是c++编写 集成 sass-loader 把scss装换成css css-loader 找出@import和url()导入的语法,告诉w ...
- 项目记事【StreamAPI】:使用 StreamAPI 简化对 Collection 的操作
最近项目里有这么一段代码,我在做 code-review 的时候,觉得可以使用 Java8 StreamAPI 简化一下. 这里先看一下代码(不是源码,一些敏感信息被我用其他类替代了): privat ...
- Log4j官方文档翻译(八、文件输出)
使用org.apache.log4j.FileAppender可以把日志写到文件中: FileAppender配置 immediateFlush 这个标志默认为true,是否每次有消息产生都自动flu ...
- 用 Ipe 画图
如何画一个箭头 首先要了解一个概念:「path object」. Path objects are objects that are formed with lines or curves, exam ...
- GDOI2018 爆零记,Challenge Impossibility
蒟蒻的GDOI又双叒叕考挂啦...... Day 0 && Day -1 学校月考,貌似考的还不错? 然而考完试再坐船去中山实在是慢啊......晚上10点才到酒店 wifi差评... ...
- [MUTC2013][bzoj3513] idiots [FFT]
题面 传送门 思路 首先有一个容斥原理的结论:可以组成三角形的三元组数量=所有三元组-不能组成三角形的三元组 也就是说我们只要求出所有不能组成三角形的三元组即可 我们考虑三元组(a,b,c),a< ...
- redis学习(四)redis持久化之RDB、AOF
redis是内存数据库,它把数据存储在内存中,这样在加快读取速度的同时也对数据安全性产生了新的问题,即当redis所在服务器发生宕机后,redis数据库里的所有数据将会全部丢失.为了解决这个问题,re ...