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的更多相关文章

  1. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  2. leetcode 55. 跳跃游戏 及 45. 跳跃游戏 II

    55. 跳跃游戏 问题描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...

  3. 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 ...

  4. [Leetcode][Python]45: Jump Game II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...

  5. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  6. LeetCode 45. 跳跃游戏 II | Python

    45. 跳跃游戏 II 题目来源:https://leetcode-cn.com/problems/jump-game-ii 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...

  7. Java实现 LeetCode 45 跳跃游戏 II(二)

    45. 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...

  8. [leetcode] 45. 跳跃游戏 II(Java)(动态规划)

    45. 跳跃游戏 II 动态规划 此题可以倒着想. 看示例: [2,3,1,1,4] 我们从后往前推,对于第4个数1,跳一次 对于第3个数1,显然只能跳到第4个数上,那么从第3个数开始跳到最后需要两次 ...

  9. [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 ...

随机推荐

  1. 解决HTML textarea 标签出现大量空格

    就是什么内容也不写,然后前面却有一堆空格 原因是 textarea标签换行了 <textarea cols=" id="serve_content" name=&q ...

  2. iOS开发之NSTimer

    1.NSTimer叫做“定时器”,它的作用如下 Ø 在指定的时间执行指定的任务 Ø 每隔一段时间执行指定的任务 2.调用NSTimer下面的方法就会开启一个定时任务 + (NSTimer *)sche ...

  3. httpclient源码分析之 PoolingHttpClientConnectionManager 获取连接

    PoolingHttpClientConnectionManager是一个HttpClientConnection的连接池,可以为多线程提供并发请求服务.主要作用就是分配连接,回收连接等.同一个rou ...

  4. Linux 搭建svn版本库

    一.安装svn服务器端yum install subversion      从镜像下载安装svn服务器端 如果后面执行“svnadmin create /usr/local/svn/sunny”提示 ...

  5. 【转】PV3D的小练习~太阳系八大行星

    转自:http://hi.baidu.com/boycy/item/70d1ba53bc8c3a958c12eddf http://www.cnblogs.com/flash3d/archive/20 ...

  6. 老李分享:走读unittest源码

    老李分享:走读unittest源码   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.poptest测试开发工程师就业培训感兴趣 ...

  7. C语言编码风格_集锦_1

    参考原地址: http://www.jb51.net/article/79257.htm <一> 在一个标准的C语言程序中, 最特殊的莫过于main函数了. 函数大体上分为内联函数(C99 ...

  8. Java开发—乘风破浪

    最近需要上线很多新的JAVA项目,然而很多JAVA的相关库都不太熟悉,项目实现起来遇到了不小阻力,熬了好几天夜.手头的基本完成了,因此打算好好来熟悉下java的相关工具库,需要借助你们,好好的在JAV ...

  9. [转载]转载一篇好文章作为Java与面向对象之随感(3)

    关于对象与引用之间的一些基本概念. 初学Java时,在很长一段时间里,总觉得基本概念很模糊.后来才知道,在许多Java书中,把对象和对象的引用混为一谈.可是,如果我分不清对象与对象引用, 那实在没法很 ...

  10. struts2 之 struts2数据处理

    开门见山,struts2的数据处理总结: 1. 在servlet中,如果要获取页面提交的数据要使用requerst.getParameter方法来获取,并且每次需要进行相关的类型转换工作,数据的获取及 ...