55 Jump Game i && 45 Jump Game ii
Jump Game
Problem statement:
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.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
Analysis:
There are two solutions for this problem, one is greedy, another is dynamic programming. The main difference is the direction.
Solution one:
Greedy is the best solution for this problem, it is always forwarding(AC) O(n).
- Loop the whole array
- Keep a right most position where I can get, update it at each index.
- if right most position is always greater than current index or it is already exceed the last position of array, return true since we can get the last position
- Once current index is greater than right most position, return false, since there is already no way to get there.
The code is as following:
class Solution {
public:
bool canJump(vector<int>& nums) {
if(nums.empty()){
return false;
}
// keep a indicator for current right most position we can reach
int right_most = ;
// loop to enumrate all elements
for(int ix = ; ix < nums.size(); ix++){
// if current element already exceed the right most position
// return false
if(right_most < ix){
return false;
} else {
// we already could reache the last element
if(ix + nums[ix] >= nums.size() - ){
return true;
} else {
// otherwise, update the right most position
right_most = max(ix + nums[ix], right_most);
}
}
}
return false;
}
};
Solution two(NOT AC):
Dynamic programming O(n*n)
For dynamic programming, we looks back, for each element, we enumerate all the element whose index is lower than it, and check if it is reachable.
class Solution {
public:
// dynamic programming solution
bool canJump(vector<int>& nums) {
if (nums.empty()) {
return false;
}
int size = nums.size();
vector<bool> true_table(size, false);
true_table[] = true;
for(int i = ; i < nums.size(); i++){
for(int j = ; j < i; j++){
if(true_table[j] && nums[j] + j >= i){
true_table[i] = true;
break;
}
}
}
return true_table[size - ];
}
};
--------------------------------- divide line --------------------------------------------
Jump Game ii
Problem Statement:
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.)
Note: You can assume that you can always reach the last index.
Analysis:
The main difference between jump game i && ii is that we should keep a minimum jump array for each element and update it for each element.
Solution one: Greedy
This is the accepted solution.
Solution two: Dynamic programming(NOT AC)
class Solution {
public:
int jump(vector<int>& nums) {
if(nums.empty()){
return ;
}
int size = nums.size();
vector<bool> can_jump(size, false);
vector<int> min_jump(size, INT_MAX);
// initialize start status
can_jump[] = true;
min_jump[] = ;
// dynamic programming
for(int i = ; i < nums.size(); i++){
for(int j = ; j < i; j++){
if(can_jump[j] && nums[j] + j >= i){
can_jump[i] = true;
min_jump[i] = min(min_jump[i], min_jump[j] + );
}
}
}
// return end status
return min_jump[size - ];
}
};
Solution two: Greedy(AC)
we keep two variables, the first one is the most right position in current jump, the second one is the right most position in next jump.
Just one loop to get the final solution:
class Solution {
public:
int jump(vector<int>& nums) {
// initialize
if(nums.size() < ){
return ;
}
// variables
int cur_jump_right_most = nums[];
int next_jump_right_most = ;
int min_jump = ;
if(cur_jump_right_most >= nums.size() - ){
return min_jump;
}
// O(n)
for(int ix = ; ix < nums.size(); ix++){
if(ix > cur_jump_right_most){
// at boundary
// update the cur_jump_right_most position before next_jump_right_most
cur_jump_right_most = next_jump_right_most;
min_jump++;
}
next_jump_right_most = max(next_jump_right_most, nums[ix] + ix);
if(next_jump_right_most >= nums.size() - ){
return ++min_jump;
}
}
return min_jump;
}
};
55 Jump Game i && 45 Jump Game ii的更多相关文章
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- leetcode 55. 跳跃游戏 及 45. 跳跃游戏 II
55. 跳跃游戏 问题描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...
- Leetcode 55. Jump Game & 45. Jump Game II
55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- LeetCode 45. 跳跃游戏 II | Python
45. 跳跃游戏 II 题目来源:https://leetcode-cn.com/problems/jump-game-ii 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...
- Java实现 LeetCode 45 跳跃游戏 II(二)
45. 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- [leetcode] 45. 跳跃游戏 II(Java)(动态规划)
45. 跳跃游戏 II 动态规划 此题可以倒着想. 看示例: [2,3,1,1,4] 我们从后往前推,对于第4个数1,跳一次 对于第3个数1,显然只能跳到第4个数上,那么从第3个数开始跳到最后需要两次 ...
- [LeetCode#55, 45]Jump Game, Jump Game II
The problem: Given an array of non-negative integers, you are initially positioned at the first inde ...
随机推荐
- 解决使用JavaScriptConvert转换对象为Json时,中文和&符号被转码的问题
描述:比如 对象为var data=new {url="http://www.baidu.com?a=b&c=d"} 使用JavaScriptConvert序列化为JSON ...
- SHA1l加密
public static string SHA1Encrypt(string sourceText) { SHA1 sha = new SHA1CryptoServiceProvider(); AS ...
- FDG内存分配器笔记
FDG: 大规模并行系统中的动态内存分配器由于需要全局同步(记账) ,导致性能急剧下降. 代码解析 1.superblock 类中包含两个变量,两个函数.默认superblock大小为2048 ite ...
- vs打开项目出错:未找到导入的项目“C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\BuildCustomizations\CUDA 5.0.props”的解决办法
有时候由于CUDA升级或者下载的源码原创建项目的CUDA版本与自己的不同,在打开项目的时候发现加载不上,提示:未找到导入的项目“C:\Program Files (x86)\MSBuild\Micro ...
- 关于window service2008系统iis部署访问证书,内部错误
近期因为在做微信支付系列,做到退款的时候需要通过把数据流通过证书post过去的时候,win7.win8.xp部署在iis都没问题.但是部署到服务器 2008的时候就出现了内部错误. 折腾许久,总算找到 ...
- SSH自动断开连接的原因、配置(转)
方法一: 用putty/SecureCRT连续3分钟左右没有输入, 就自动断开, 然后必须重新登陆, 很麻烦. 在网上查了很多资料, 发现原因有多种, 环境变量TMOUT引起,ClientAliveC ...
- 【一通百通】c/php的printf用法
1.先说说PHP printf()函数: printf()函数的调用格式为: printf("<格式化字符串>", <参量表>); %d 十进制有符号整数 ...
- 2017-3-28 JavaScript 基础、语法
前端三剑客: html+css+js(html 决定网页上有什么,css决定东西是怎么摆放的,js决定东西的功能) js定义: js是一个脚本语言,需要有宿主文件,它的宿主文件是html文件. js ...
- 服务器中 配置phpstudy一键安装包
在线phpstudy一键安装包 安装版: (很简单) wget -c http://lamp.phpstudy.net/phpstudy.bin chmod +x phpstudy.bin ...
- Poptest学员之当小厨师变成测试开发工程师
没开玩笑,这是我们的真实案例.做培训以来,各行各业转行做测试的学员见得太多了.修车的.客服的.销售的.司机的.医护的.前台的等等.职位虽然不分贵贱,但是薪资却分多少.每个人心中都有让家人和自己过上好日 ...