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 ...
随机推荐
- 科学计算库Numpy——排序
矩阵按维度排序 使用np.sort()进行排序. 排序索引值 使用np.argsort()排序,返回排序后的索引值. 备注:array1[1,2]=1.2,array1[1,0]=5.6,array1 ...
- day25-python之继承组合
1.上节回顾 class School: x=1 def __init__(self,name,addr,type): self.Name=name self.Addr=addr self.Type= ...
- TI C6000优化手册——让代码看起来像钉子
DSP芯片的出现,是为了解决大量的数字运算问题.通过集成专用的加法器.乘法器.地址产生器.复杂逻辑等硬件单元,DSP能实现比普通单片机更快速的数字运算,使处理器更适用于实时性高.复杂度强的处理场合.也 ...
- 快速排序算法(C)
sort快排函数的基本版,效率n*logn,快排的完全版就是在递归之中夹杂对序列的预判断,最优的选择排序方法,快速排序算法只是其中之一. 简单的说明一下快速排序的思想,对于一个数列,首先选择一个基数( ...
- TCP/IP网络编程之套接字与标准I/O
标准I/O函数 标准标准I/O函数有两个优点: 标准I/O函数具有良好的移植性 标准I/O函数可以利用缓冲提高性能 关于移植性无需过多解释,不仅是I/O函数,所有标准函数都具有良好的移植性.因为,为了 ...
- cf980d Perfect Groups
题意 定义一个串的权值是将其划分成 \(k\) 组,使得每一组在满足"从组里选出一个数,再从组里选出一个数,它们的乘积没有平方因子"这样的前提时的最小的 \(k\).每组的数不必相 ...
- 使用Jmeter做性能测试
上周刚刚做完项目的性能测试.今天整理和总结一下,随便分享给大家. 首页呢,测试前,我们是有明确的性能指标的,而且测试环境和数据都已准备好,业务分析.场景分析大家根据自己的项目系统进行分析设计,我们选用 ...
- 使用 htaccess 重写 url,隐藏查询字符串
例如我们有如下 URL: http://example.com/users.php?name=tania 但是我们想要让 URL 变成如下: http://example.com/users/tani ...
- PAT1033
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及坏掉的那些键,打出的结果文字会是怎样? 输入格式: 输入在2行中分别给出坏掉的那些键.以及应该输入的文 ...
- koa2 + webpack 热更新
网上有很多express+webpack的热更新,但是koa2的很少,这两天研究了一下子,写一个简单的教程. 1.需要的包 webpack:用于构建项目 webpack-dev-middleware: ...