55. Jump Game(Medium)&& 45. Jump Game II(Hard)

来源:互联网 发布:网络教育哪个好 编辑:程序博客网 时间:2024/05/19 19:42

原题目:
Jump Game:
   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. 

Jump Game II:
   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.

题目大意如下:
  给定一个含有非负整数的数组,你的初始位置设定在数组的第一个元素,数组中的每个元素代表着你能从当前位置跳跃的最大距离,第一题是要求判断能否到达最后一个元素,第二题要求返回到达最后一个元素要跳跃的最小次数(每个数组保证能到达最后一个位置)。

解题思路:
  两道题的解题思路都比较清晰——只要能保证每次做出的决策能向右前进最大距离即可(贪心算法)。
  拿第一题中的例子来说:
  A = [2,3,1,1,4]
  从A[0]=2开始,能到达A[1]、A[2];若选择到A[1]=3,则下次能到达A[2]、A[3]和A[4],A[4]为终点,算法结束;若选择A[2]=1,则下次能到达A[3]和A[2],因为向回跳虽然能够得到同样的结果但步数显然会变多所以一直不选择向回跳。显然以上两种选择中选择A[1],最终可以到达A[4],但选择A[2]只能到达A[3]。当前步的最优选择肯定就是跳到A[1]。
  每次做完跳跃之后需要检测(假设当前跳到下标为i的元素):
  ①如果A[i]=0且A[i]不是最后一个元素,则不能到达最后一个元素;
  ②如果A[i]=0且A[i]是最后一个元素,则可以到达最后一个元素(第二题直接返回计数器count);
  ③如果满足A[i]+i >= array.size()-1,则可以到达最后一个元素(第二题返回count++);
  另外有两个特殊情况:
  ①当数组为空,返回true(第二题返回count=0);
  ②当数组只有一个元素,返回true(第二题返回count=0)。

代码如下:
第一题:

class Solution {public:    bool canJump(vector<int>& nums) {        int next_pos = 0 ;        if(nums.empty()) return false ;        if(nums.size() == 1) return true ;        while(1){            //寻找最优的下一步            next_pos = find_next(next_pos , nums) ;            //若不为0,检测能否到最后一个元素            if(next_pos + nums[next_pos] >= nums.size()-1) return true ;            //若为0,检测是否为最后一个元素            if(nums[next_pos] == 0) {                if(next_pos == nums.size() - 1) return true ;                else return false ;            }             //若都不是,继续向前跳跃        }    }    int find_next(int current_pos , vector<int>& nums){        int next_pos = 0 , farest = 0 ;        for(int i = 1 ; i <= nums[current_pos] ; ++i){            if(i+current_pos < nums.size() && nums[i+current_pos]+i+current_pos > farest){                //记录当前跳的最远的点                next_pos =  i + current_pos ;                //记录下当前能跳的最远的距离                farest = nums[i+current_pos] + i + current_pos ;            }        }        return next_pos ;    }};

第二题:

class Solution {public:    int jump(vector<int>& nums) {        int next_pos = 0 , count = 0 ;        if(nums.empty() || nums.size() == 1) return 0 ;        while(1){            if(nums[next_pos] + next_pos >= nums.size() - 1) return count + 1 ;            next_pos = find_next(next_pos , nums) ;            count++ ;        }    }    int find_next(int current_pos , vector<int>& nums){        int next_pos = 0 , farest = 0 ;        for(int i = 1 ; i <= nums[current_pos] ; ++i){            if(i+current_pos < nums.size() && nums[i+current_pos]+i+current_pos > farest){                next_pos =  i + current_pos ;                farest = nums[i+current_pos] + i + current_pos ;            }        }        return next_pos ;    }};

运行结果:
第一题:
运行结果1
第二题:
运行结果2

算法分析:
  最坏情况O(n²),最好O(n)。

0 0
原创粉丝点击