45-Jump Game II

标签: 算法 数据结构 leetcode
10人阅读 评论(0) 收藏 举报
分类:

Description:
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.


Example:

Input: [2,3,1,1,4]
Output: 2
Explanation: 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.


问题描述

给定一个正整数数组,你最初在数组起始下标。
数组中每个元素代表你在当前下标处的最大的跳跃距离。
返回到达数组最后的下标所需的最少的跳跃数。

注意,输入确保你总能到达最后的下标。


我的做法是通过动态规划

解法1(动态规划)

class Solution {
    public int jump(int[] nums) {
        int len = nums.length;
        int[] dp = new int[len];
        //对数组初始化为最大值
        for(int i = 0;i < len;i++)  dp[i] = len;
        dp[0] = 0;

        for(int i = 1;i < len;i++){
            for(int j = 0;j < i;j++){
                //条件判断代表j下标可以跳跃到i并且可以使到达i的步数变少
                if(j + nums[j] >= i && dp[j] < dp[i]){
                    dp[i] = Math.min(dp[i], dp[j] + 1);
                    break;
                }
            }
        }

        return dp[len - 1];
    }
}

更好的做法,贪心
解法2(贪心)

class Solution {
    public int jump(int[] A) {
        /*
        step_count代表总步数
        last_jump_max代表上一个最远界
        current_jump代表正在被更新,下次使用的最远界
        */
        int step_count = 0, last_jump_max = 0, current_jump_max = 0;

        //迭代,更新current_jump,若到达上次最远界,jump增1,将last_jump_max置为current_jump_max
        for(int i = 0; i < A.length - 1; i++) {
            current_jump_max = Math.max(current_jump_max, i + A[i]);
            if( i == last_jump_max ) {
                step_count++;
                last_jump_max = current_jump_max;
            } 
        }

        return step_count;
    }
}
查看评论

【LeetCode】Jump Game II 解题报告

【题目】 Given an array of non-negative integers, you are initially positioned at the first index o...
  • ljiabin
  • ljiabin
  • 2014-12-09 17:17:02
  • 3791

Jump Game II -- LeetCode

原题链接: http://oj.leetcode.com/problems/jump-game-ii/  这道题是Jump Game的扩展,区别是这道题不仅要看能不能到达终点,而且要求到达终点的最少...
  • linhuanmars
  • linhuanmars
  • 2014-03-20 23:25:59
  • 15108

[LeetCode]Jump Game II(贪心&&DP!!!!!)

Given an array of non-negative integers, you are initially positioned at the first index of the a...
  • whzyb1991
  • whzyb1991
  • 2015-07-18 13:27:47
  • 476

LeetCode_Jump Game&&Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the arra...
  • loveRooney
  • loveRooney
  • 2014-08-09 12:46:38
  • 9833

LeetCode | Jump Game II

题目: Given an array of non-negative integers, you are initially positioned at the first index of th...
  • lanxu_yy
  • lanxu_yy
  • 2013-09-15 21:26:56
  • 6959

LeetCode 45. Jump Game II|贪心算法

题目描述Given an array of non-negative integers, you are initially positioned at the first index of the ...
  • tbsjianjian
  • tbsjianjian
  • 2016-11-14 00:47:47
  • 320

[LeetCode294] Flip Game II

Flip Game II -- LeetCode 294 You are playing the following Flip Game with your friend: Given a strin...
  • sbitswc
  • sbitswc
  • 2015-10-18 08:08:17
  • 4448

lintcode(666)Guess Number Game II

Description: We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. Y...
  • sunday0904
  • sunday0904
  • 2017-07-04 10:13:21
  • 1032

LeetCode --- 45. Jump Game II

题目链接:Jump Game II Given an array of non-negative integers, you are initially positioned at the firs...
  • makuiyu
  • makuiyu
  • 2015-02-10 08:59:21
  • 1166

Leetcode 293. Flip Game & 294. Flip Game II

293. Flip Game Total Accepted: 14847 Total Submissions: 28238 Difficulty: Easy Contributors: Adm...
  • fantasiasango
  • fantasiasango
  • 2016-10-17 06:58:46
  • 932
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 2万+
    积分: 6362
    排名: 4961
    博客专栏
    文章存档
    最新评论