关闭

《leetCode》:Jump Game II

标签: leetcode游戏贪婪算法
280人阅读 评论(0) 收藏 举报
分类:

题目

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

题目大意:找出到达最后一个元素需要的最小的步数时多少??

思路

思路来源于leetCode中本题的讨论组。
应用贪婪算法。
有一点需要说明的是:所有测试用例必须是能够到达终点的,不允许出现不可达的这种情况:[0,1,2]

The ideas is simple ,using some greedy tactics.

We generally maintain a start position to indicate the current position,and there are only 2 main steps for solving the problem.

step(1): When you are at position start, you can go to positon start + 1 to start + nums[start] if nums[start] > 0, 
and now you can know whether you can go to the end by judging nums[start] >= nums.size()-1.
If so, go to step(3). If not, go to step(2).

step(2): From start + 1 to start + nums[start], in these nums[start] positions,pick one to be the next start position. 
And the rule for picking is simple:

You pick a position start + i, from which you can move forward the longest distance,
which means i + nums[start + i] >= j + nums[start + j] for any 1 <= j <= nums[start].

So the start position is at start + i now. Then you go to the step(1).

step(3): Return the answer.

实现代码如下:

int count;
void minJump(int *nums,int numsSize,int start){
    if(nums==NULL||numsSize<1){
        return 0;
    }
    count++;
    int end=start+nums[start];
    if(end>=numsSize-1){
        return ;
    }
    int max=0;
    int index=0; 
    for(int i=start+1;i<=end;i++){//寻找在这个区间中,离目标最近的那个点 
        if(i+nums[i]>max){
            index=i;
            max=i+nums[i];
        }
    }
    minJump(nums,numsSize,index);//起点换了 


}

int jump(int* nums, int numsSize) {
    if(nums==NULL||numsSize<1){
        return 0;
    }
    if(numsSize==1){//一个元素
        return 0;
    }
    count=0;
    minJump(nums,numsSize,0);
    return count;

}

最后AC结果如下:

1
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

数据结构与算法[LeetCode]——Jump Game I II

Jump Game II Given an array of non-negative integers, you are initially positioned at...
  • SUN20082567
  • SUN20082567
  • 2014-04-14 19:39
  • 387

【算法分析与设计】【第三周】679. 24 Game

题目来源:https://leetcode.com/problems/24-game/ 周三刚复习了DFS,这周就做点有意思的题——求24点。相信求24点是大家的童年回忆,玩法也很简单:...
  • raoyx
  • raoyx
  • 2017-09-23 12:10
  • 208

【算法】【Greedy】Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the arra...
  • wurlin
  • wurlin
  • 2017-12-28 12:31
  • 15

经典算法——Jump Game(II)

Given an array of non-negative integers, you are initially positioned at the first index of the arra...
  • geekmanong
  • geekmanong
  • 2016-04-08 15:14
  • 751

LeetCode 之 Jump Game(贪心算法)

LeetCode 之 Jump Game,本文给出基于动态规划思想、贪心算法的三种实现思路分析,最后,贴上三套C++详细源码实现。
  • u013630349
  • u013630349
  • 2015-08-05 11:05
  • 905

【LeetCode】Jump Game (一维动态规划 + 线性扫描)

Given an array of non-negative integers, you are initially positioned at the first index of the arra...
  • xiaozhuaixifu
  • xiaozhuaixifu
  • 2013-10-30 15:23
  • 6632

LeetCode OJ 之 Jump Game II(“跳跃”游戏 - 二)

题目: Given an array of non-negative integers, you are initially positioned at the first index of...
  • u012243115
  • u012243115
  • 2015-01-13 08:27
  • 457

【leetcode】Jump Game I, II 跳跃游戏一和二

题目: Jump Game I: Given an array of non-negative integers, you are initially positioned at th...
  • u012204343
  • u012204343
  • 2014-08-28 00:55
  • 923

LeetCode OJ算法题(四十四):Jump Game II

题目: Given an array of non-negative integers, you are initially positioned at the first index of...
  • op_yu
  • op_yu
  • 2014-08-06 12:43
  • 308

Leetcode 45. Jump Game II dp优化 (给出了5种思路)

题意 给你一个n长的数组a,a[i]表示从i跳一步,最远距离 问你,从0出发跳到n-1的位置最少需要几步 思路 这题挺有意思的,我想了4个思路,2个T了,1个实现起来比较麻烦。。就记录下几个思路,感觉...
  • luke2834
  • luke2834
  • 2016-11-05 13:42
  • 151
    个人资料
    • 访问:643571次
    • 积分:12281
    • 等级:
    • 排名:第1374名
    • 原创:606篇
    • 转载:14篇
    • 译文:0篇
    • 评论:111条
    联系方式
    有问题欢迎探讨咨询哈
    qq号就不留了哈
    欢迎留言
    博客专栏
    最新评论