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 ...
随机推荐
- Codeforces Gym 100269E Energy Tycoon 贪心
题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...
- 谈谈一些有趣的CSS题目(十三)-- 巧妙地制作背景色渐变动画!
开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...
- js正则表达式详解及示例讲解
所谓正则表达式,简单来说就是一种规则,一种计算机能读懂的规则.js中的正则表达式语法是Perl5(一种很早的编程语言)的正则语法的子集.本文将在基础知识的基础上添加示例帮助快速理解正则表达式. 学习正 ...
- flex中为控件添加监听器并计算
1.添加监听器: public function moduleCreationComplete():void { this.D601_29a.addEventListener(FlexEvent.SE ...
- es6 箭头函数(arrow function) 学习笔记
箭头函数有两个好处. 1.他们比传统函数表达式简洁. const arr = [1, 2, 3]; const squares = arr.map(x => x * x); // 传统函数表达式 ...
- vue学习笔记 概述(一)
vue里 最常见的 最普遍的用法 应该是 var app = new Vue({ el: '#app', data: { message: 'Hello Vue!' }}) 下面把所有使用方法尽可能列 ...
- ASP.NET MVC制作404跳转(非302和200)
前言:距离上次发文已经有几个月了! 这段时间李,制作了一个博客网站,现将博客文章选一些发表到博客园,顺便为自己网站打一下广告! 产生404的原因主要有以下: 1.浏览器和爬虫:某些浏览器会请求网站的f ...
- sqlcmd的使用小结
据说,超过80M的sql文件是不能在查询分析器中执行的(可能是运行得太慢,也可能查询分析器就不能容载如此多的语句). 那么就有了sqlcmd命令: 首先进入cmd窗口,便可进行以下操作 1.登录sql ...
- CSS3学习笔记(4)-CSS3函数
p{ font-size: 15px; text-indent: 2em; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid ...
- Android内存优化之OOM
内容大多都是和OOM有关的实践总结概要.理解错误或是偏差的地方,还请多包涵指正,谢谢!本人Q:1524447071 (一)Android的内存管理机制 Google在Android的官网上有这样一篇文 ...