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 ...
随机推荐
- Python中的变量
多个变量赋值 Python允许你同时为多个变量赋值.例如: a = b = c = 1 以上实例,创建一个整型对象,值为1,三个变量被分配到相同的内存空间上. 您也可以为多个对象指定多个变量.例如: ...
- 读learning spark lighting chapter1~chapter2
chapter 1 introduction to the analysis with spark the conponents of Sparks spark core(contains the b ...
- Windows搭建以太坊的私有链环境
1.下载Geth.exe 运行文件,并安装 https://github.com/ethereum/go-ethereum/releases/ 下载后,只有一个Geth.exe的文件 2.cmd进入按 ...
- iOS开发之UIDevice通知
UIDevice类提供了一个单例对象,它代表着设备,通过它可以获得一些设备相关的信息,比如电池电量值(batteryLevel).电池状态(batteryState).设备的类型(model,比如iP ...
- 细心!SQL语句进行运算时使用字符串时缺失精度的细节!
昨天没有更新,特此来说明下原因,昨天回到家时已经甚晚,正逢公司这几天项目比较紧张(bug多,赶需求,看着bug单齐刷刷的转过来,心都颤抖了一下),没有及时准备素材,今天又加了一天班(现在还在公司,偷个 ...
- css——样式表分类,选择器
一,样式表分类 (1)内联样式[优先级最高][常用][代码重复使用性最差] (当特殊的样式需要应用到个别元素时,就可以使用内联样式. 使用内联样式的方法是在相关的标签中使用样式属性.样式属性可以包含任 ...
- ActiveMQ Part 1 : 基本安装配置(windows 版本)
1. 安装启动服务 A) 首先下载并安装最新的 JDK(本文使用:jdk-8u66-windows-x64.exe) B) 从官网下载最新的安装包(本文下载版本为:http://activemq.ap ...
- linux最常用命令
1,cd命令 作用:切换当前目录,它的参数切换的路劲,可以是相对路劲,也可以是绝对路劲. 用法: cd /root/Docements #切换当/root/Docements,绝对路劲 cd ./ ...
- Object-C定时器,封装GCD定时器的必要性!!! (一)
实际项目开发中经常会遇到延迟某件任务的执行,或者让某件任务周期性的执行.然后也会在某些时候需要取消掉之前延迟执行的任务. iOS中延迟操作有三种解决方案: 1.NSObject的方法:(对象方法) p ...
- js 操作属性
操作属性: 对象.setAttribute('属性名','值'); - 添加属性 对象.getAttribute('属性名'); - 获取属性值,如无此属性,那么返回null 对象.removeAtt ...