LeetCode: Jump Game II 解题报告

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

SOLUTION 1:

参考:http://blog.csdn.net/fightforyourdream/article/details/14517453

我们可以使用贪心法来解决这个问题。

从后往前思考问题。以 des = len - 1反向思考。思考能到达它的最远的距离是多少。

例子:

2 3 1 1 4

i = 1

上例子中index i = 2是达到4的最远的距离。这时index i = 1 到4是最后一步的最优解,因为其它的解,如果跳到index = 2, 3 到达4为最后一步,那么倒数第二步既然可以到达 index = 2, 3 也可以到达index = 1,所以跳到index = 1步数会是一样的。所以其它的解最好的情况也就是与从index = 2跳过去一样而已。

而前面的点因为距离限制 ,有可能只能跳到index = 1,而不可以跳到index = 2, 3.所以 将倒数第二步设置在index = 1可以得到最多的解。

 1 package Algorithms.greedy;
 2
 3 public class Jump {
 4     public static void main(String[] strs) {
 5         int[] A = {2, 3, 1, 1, 4};
 6         System.out.println(jump(A));
 7     }
 8
 9     public static int jump(int[] A) {
10         if (A == null || A.length == 0) {
11             return 0;
12         }
13
14         int len = A.length;
15
16         int sum = 0;
17
18         int des = len - 1;
19         while (des > 0) { // destination index
20             for (int i = 0; i < des; i++) { // 不断向前移动dest
21                 if (A[i] + i >= des) { // 说明从i位置能1步到达dest的位置
22                     sum++;
23                     des = i; // 更新dest位置,下一步就是计算要几步能调到当前i的位置
24                     //break; // 没必要再继续找,因为越早找到的i肯定越靠前,说明这一跳的距离越远
25                              // 这一行可以去掉, des = i,不符合for的条件,会自动break.
26                     System.out.println("sum:" + sum);
27                     System.out.println("des:" + des);
28                 }
29             }
30         }
31
32         return sum;
33     }
34 }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/greedy/Jump.java

时间: 2024-12-14 13:08:08

LeetCode: Jump Game II 解题报告的相关文章

【LeetCode】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

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

【原创】leetCodeOj --- Jump Game II 解题报告

原题地址: https://oj.leetcode.com/problems/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 goa

LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]] SOLUTION 1: 还是与上一题Spiral Matrix类似

LeetCode: Word Break II 解题报告

Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat",

LeetCode: Palindrome Partitioning II 解题报告

Palindrome Partitioning II Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome pa

LeetCode: Combination Sum II 解题报告

Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note:All numbers (including ta

LeetCode: Path Sum II 解题报告

Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] SOLUTION 1: 使用