# 跳跃游戏

## LeetCode 55. 跳跃游戏

[LeetCode 55. 跳跃游戏](https://leetcode-cn.com/problems/jump-game/)

### 题目

```javascript
给定一个非负整数数组 nums ，你最初位于数组的 第一个下标 。

数组中的每个元素代表你在该位置可以跳跃的最大长度。

判断你是否能够到达最后一个下标。



示例 1：

输入：nums = [2,3,1,1,4]
输出：true
解释：可以先跳 1 步，从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。
示例 2：

输入：nums = [3,2,1,0,4]
输出：false
解释：无论怎样，总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 ， 所以永远不可能到达最后一个下标。
```

### 题解

#### 贪心算法

```javascript
function canJump(nums: number[]): boolean {
    if(nums.length <= 1) return true;

    let maxDepth:number = 0;

    // 更新前i个点最远能到达的点
    for(let i=0;i<nums.length-1;i++){
        maxDepth = Math.max(nums[i]+i,maxDepth)
        // 之前点的最大到达距离都到不了这个点，就卡住了
        if(i >= maxDepth) return false; 
        if(maxDepth >= nums.length-1) return true;
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://voyz.gitbook.io/voyz-algorithm/tan-xin-suan-fa/tiao-yue-you-xi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
