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 ...
随机推荐
- MQTT的学习之Mosquitto发布-订阅(2)
在<MQTT的学习之Mosquitto安装&使用(1)>一文末尾,我已经模拟了发布-订阅模式,只是那时在服务器直接模拟的,并不是java代码模拟的.下面贴出Java代码 1.首先引 ...
- DFS:Tempter of the Bone (规定时间达到规定地点)
解题心得: 1.注意审题,此题是在规定的时间达到规定的地点,不能早到也不能晚到.并不是最简单的dfs 2.在规定时间达到规定的地点有几个剪枝: 一.公式:所需的步骤 - x相差行 - y相差列 = 偶 ...
- Python之print函数详解
输出的 print 函数总结: 1. 字符串和数值类型可以直接输出 >>> print(1) 1 >>> print("Hello World" ...
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 19: ordinal not in range(128)
解决方案: 1: 在网上找到的解决方案是: 在调用import matplotlib.pyplot as plt前 import sys sys.setdefaultencoding(“gbk”) 让 ...
- windows服务安装卸载
到C盘下找到对应的开发VS的installutil.exe文件,复制到程序的执行文件(*.exe)相同目录下在开始程序中找到VS命令提示工具 转到程序的执行文件(*.exe)目录下 C:\>cd ...
- Linux之匿名FTP服务器搭建
FTP(File Transfer Protocol)是在服务器与客户端进行文件传输的一种传输协议.本次介绍的是vsftpd的软件体验ftp服务. FTP服务器默认情况下依据用户登录情况分为三种不同的 ...
- 【Combinations】cpp
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- [oldboy-django][5python基础][高级特性]Iterator迭代器
# 区分可迭代对象iterable, 迭代器iterator, 生成器generator a. iterable 可直接用for循环的对象,都称为可迭代对象, from collections imp ...
- docker log 批量删除报错: find: `/var/lib/docker/containers/': 没有那个文件或目录
问题描述: 服务器上面docker log太多,打算用之前写的批量清理shell脚本清理掉,但是发现报错. find: `/var/lib/docker/containers/': 没有那个文件或目录 ...
- linux开机关机自启动或自关闭服务的方式
背景 由于迁移部门jira和confluence到linux,需要设置这两个服务在开机或关机时能够自动启动或关闭.这里我就拿配置confluence过程来记录. 方式1:chkconfig 特点:ch ...