Problem Description

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.

Return true if you can reach the last index, or false otherwise.

Examples

Example 1:

Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.

Constraints:

解析

給定一個非負整數陣列 nums

每個元素 nums[i] 代表在 從 index 出發最多能夠 jump 的步數

初始從 index = 0 開始

要求寫一個演算法判斷給定的 nums 能不能 jump 到最後一個 index, 也就是 index = len(nums) - 1

從思考何時會導致無法跑到最後一個 index

當發現有一個 index 從該位找出的 nums[index] + index

與之前累計的最大能前進的 index 都無法超過目前的 index