📉
leetcode-题解
  • leetcode-notes
  • linked-list
    • 2-Add-Two-Numbers
    • 109-Convert-Sorted-List-to-Binary-Search-Tree
    • 19-Remove-Nth-Node-From-End-of-List
    • 92-Reverse-Linked-List-II
    • 142-Linked-List-Cycle-II
    • 83-Remove-Duplicates-from-Sorted-List
    • 61-Rotate-List
    • 148-Sort-List
    • 86-Partition-List
    • 82-Remove-Duplicates-from-Sorted-List-II
    • 138-Copy-List-with-Random-Pointer
    • 328-Odd-Even-Linked-List
    • 23- Merge-k-Sorted-Lists
    • 25-Reverse-Nodes-in-k-Group
  • templates
    • bitmap
    • ologn
    • Morris
    • dp
    • binary-search
    • Maxwindow
    • 递归
    • union
    • graph
    • greedy-algorithm
    • kmp
    • list
    • ordered-list
    • tree
    • Manacher
    • Monotonic-stack
    • big-data
    • sort-Summary
    • Bucket-sort
    • bit-opreation
    • heap-sort
  • arrays
    • others
      • 31-Next-Permutation
      • 66-Plus- One
      • 229-Majority-Element-II
      • 414-Third-Maximum-Number
    • matrix
      • 74-Search-a-2D-Matrix
      • 289-Game-of-Life
    • PrefixOrSuffix
      • 560-Subarray-Sum-Equals-K
      • 238-Product-of-Array-Except-Self
    • 二分法
      • rotated-array-problem
      • D天内送达包裹的能力
      • 162-Find-Peak-Element
      • Minimize-maximum-and-maximize-minimum
    • 多指针
      • 611-Valid-Triangle-Number
      • 228-Summary-Ranges
      • 75-Sort-Colors
      • 18-4Sum
      • 27-Remove-Element
      • 三数之和
      • 26-Remove-Duplicates-from-Sorted-Array
      • 盛最多水的容器
      • 80-Remove-Duplicates-from-Sorted-Array-II
      • 最接近的三数之和
    • array-circle
      • 457-Circular-Array-Loop
      • 287-Find-the-Duplicate-Number
      • 565-Array-Nesting
    • 智力题
      • 73-Set-Matrix-Zeroes
      • 最佳观光组合
    • 几何问题
      • 统计全为1的正方形子矩阵
      • 495-Teemo-Attacking
    • sort
      • 88-Merge-Sorted-Array
      • 57-Insert-Interval
  • tree
    • 105-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal
    • 230-Kth-Smallest-Element in-a-BST
    • 106-Construct-Binary-Tree-from-Inorder-and-Postorder-Traversal
    • 257-Binary-Tree-Paths
    • 113-Path-Sum-II
    • 96-Unique-Binary-Search-Trees
    • 124-Binary-Tree-Maximum-Path-Sum
    • 103-Binary-Tree-Zigzag-Level-Order-Traversal
    • 426-Convert-Binary-Search-Tree-to-Sorted-Doubly-Linked-List
    • 117-Populating-Next-Right-Pointers-in-Each-Node-II
    • 99-Recover-Binary-Search-Tree
    • 366-Find-Leaves-of-Binary-Tree
    • 337-House-Robber-III
    • 333-Largest-BST-Subtree
    • 298-Binary-Tree-Longest-Consecutive-Sequence
    • 428-Serialize-and-Deserialize-N-ary-Tree
    • 1367-Linked-List-in-Binary-Tree
    • 173-Binary-Search-Tree-Iterator
    • 98-Validate-Binary-Search-Tree
    • 156-Binary-Tree-Upside-Down
    • 404-Sum-of-Lef- Leaves
    • 255-Verify-Preorder-Sequence-in-Binary-Search-Tree
    • 272-Closest-Binary-Search-Tree-Value-II
    • 95-Unique-Binary-Search-Trees-II
    • 222-Count-Complete-Tree-Nodes
    • 431-Encode-N-ary-Tree to-Binary-Tree
    • Lowest-Common-Ancestor-of-a-Binary-Tree
    • 129-Sum-Root-to-Leaf-Numbers
  • recursive
    • 前言
    • 39-Combination-Sum
    • 79-Word-Search
    • 04-Power-Set-LCCI
    • 前言
    • 90-Subsets-II
    • 40-Combination-Sum-II
    • 351-Android-Unlock-Patterns
  • dynamic-programming
    • 276-Paint-Fence
    • 132-Palindrome-Partitioning-II
    • 361-Bomb-Enemy
    • 62-Unique-Paths
    • 376-Wiggle-Subsequence
    • 403-Frog-Jump
    • 32-Longest-Valid-Parentheses
    • 97-Interleaving-String
    • 354-Russian-Doll-Envelopes
    • 279-Perfect-Squares
    • 304-Range-Sum-Query-2D-Immutable
    • 10-Regular-Expression-Matching
    • Paint-House-series
    • 139-Word-Break
    • Best-Time-to-Buy-and-Sell-Stock-series
    • 416-Partition-Equal-Subset-Sum
    • 300-Longest-Increasing-Subsequence
    • 91-Decode-Ways
    • Ugly-Number-series
    • 363-Max-Sum-of-Rectangle-No-Larger-Than-K
    • 368-Largest-Divisible-Subset
    • 63-Unique-Paths-II
    • 312-Burst-Balloons
    • 322-Coin-Change
    • 64-Minimum-Path-Sum
    • 140-Word-Break-II
    • 120-Triangle
    • 72-Edit-Distance
    • House-Robber-series
    • 413-Arithmetic-Slices
    • 174-Dungeon-Game
    • 87-Scramble-String
    • 44-Wildcard-Matching
    • 338-Counting-Bits
    • 152-Maximum-Product-Subarray
    • 375-Guess-Number-Higher-or-Lower-II
  • hash-table
    • 381-Insert-Delete-GetRandom-O(1) - Duplicates-allowed
    • 442-Find-All-Duplicates-in-an-Array
    • 380-Insert-Delete-GetRandom-O(1)
    • 1-Two-Sum
    • 3-Longest-Substring-Without-Repeating-Characters
    • 41-First-Missing-Positive
  • stack
    • Monotonic stack
      • 84-Larges-Rectangle-in-Histogram
      • 42-Trapping-Rain-Water
  • bit-manipulation
    • 08-Draw-Line-LCCI
  • Mysql
    • 185-Department-Top-Three-Salaries
    • 177-N-Highest-Salary
    • 178-Rank-Scores
    • 180-Consecutive-Numbers
  • greedy
    • 56-Merge-Intervals
    • 55-Jump-Game
    • 53-Maximum-Subarray
  • math
    • 357-Count-Numbers-with-Unique-Digits
    • 343-Integer-Break
    • 119-Pascal's-Triangle-II
  • string
    • Palindrome
      • 5-Longest-Palindromic-Substring
      • Manacher
  • sliding-window
    • 209-Minimum-Size-Subarray-Sum
Powered by GitBook
On this page
  • 0x1 题目详情
  • 0x2 解题思路
  • 0x3 代码实现
  • 0x4 课后总结

Was this helpful?

  1. greedy

55-Jump-Game

Previous56-Merge-IntervalsNext53-Maximum-Subarray

Last updated 4 years ago

Was this helpful?

0x1 题目详情

给定一个非负整数数组,你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断你是否能够到达最后一个位置。

测试用例: 示例 1: 输入: [2,3,1,1,4] 输出: true 解释: 我们可以先跳 1 步,从位置 0 到达 位置 1, 然后再从位置 1 跳 3 步到达最后一个位置。

0x2 解题思路

思路1:

我刚开始的思路是通过暴力递归求解,但是显而易见,这个递归是无后效性的,也就是当前跳几步和之前怎么跳过来的完全没有关系。递归的思路就是每到达一个点,就依次从大到小尝试可以跳跃的步数,如果跳跃后的索引能够到达最后,那么就表示我们是能够跳成功的。当然递归肯定是不会通过的,改成动态规划后能够低空飘过。

思路2:

第二种思路是看答题区得来的。就是我们需要维护一个当前能够到达的最大索引,如果最大索引能够超过最后一个位置,那么表示成功找到路径。否则如果当index>maxIndex时,表示我们当前index的位置我们是无论如何都不能到达的,自然返回false。贪心的具体解释见下图:

这是一种贪心的思路,以前没咋做过贪心的题目,这个贪心到底咋贪啊?

思路3:

思路2是从前往后遍历,而这种思路是从后往前遍历,如果当前元素不为0,那么是一定能够到达当前位置的。如果当前元素nums[j]为0,那么就在该元素之前查找是否有元素满足nums[i]>i-j,即是否有元素能直接跳过0元素。如果没有找到,则返回false。

0x3 代码实现

思路1:复杂度为$O(N^2)$

``` java "递归" class Solution { public boolean canJump(int[] nums) {

    if(nums==null || nums.length<2){
        return true;
    }
    return recur(nums,0);
}

boolean recur(int[] nums,int index){
    if(index==nums.length-1){
        return true;
    }
    if(index>=nums.length){
        return false;
    }
    if(nums[index]==0){
        return false;
    }
    //尝试跳1~nums[index]次
    boolean result=false;
    for(int i=nums[index];i>0;i--){
        result|=recur(nums,index+i);
        if(result==true){
            break;
        }
    }
    return result;
}

}

---
通过递归改过来的动态规划。

``` java "动态规划思路"
class Solution {
    public boolean canJump(int[] nums) {

        if(nums==null || nums.length<2){
            return true;
        }
        return recur(nums,0);
    }

    boolean recur(int[] nums,int index){
        //dp数组表示从i位置跳能否成功
        boolean[] dp=new boolean[nums.length];
        dp[nums.length-1]=true;

        for(int i=dp.length-2;i>=0;i-- ){
            // boolean result=false;
            //nums[i]表示能跳的步数
            for(int j=nums[i];j>0;j--){
                if((i+j)>=dp.length){
                    continue;
                }
                dp[i]|=dp[i+j];
                if(dp[i]==true){
                    break;
            }
        }
        }
        return dp[0];
    }

}

思路2:,复杂度为$O(N)$

``` java "贪心思路" class Solution { public boolean canJump(int[] nums) { if(nums== null || nums.length<2){ return true; } boolean result=false; int maxIndex=0; for(int i=0;i<nums.length-1;i++){ //如果最大索引不能到达当前位置,表示最后结果为false,不能到达最后一个位置 if(i>maxIndex){ return false; } maxIndex=Math.max(i+nums[i],maxIndex); //提前终止循环 if(maxIndex>=nums.length-1){ result=true; break; } } return result; } }

```

0x4 课后总结

总的来说呢我对做贪心的题目没啥经验感觉全靠猜啊,以后在慢慢总结把,感觉没啥套路啊。

原题链接
贪心