# 55. Jump Game

![](/files/-MeZWQrhQ7pNTnOTg2xd)

## greedy

time: O(n)

space: O(1)

```java
class Solution {
    public boolean canJump(int[] nums) {
        // use greedy 
        // max reach position index = max steps + current position = nums[i] + i
        // so in next round, if current position index > max reach position index, it cant reach this position
        
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i > max) return false;
            max = Math.max(max, nums[i] + i);
        }
        return true;
    }
}
```

```
 0 1 2 3 4 => i > max => 4 > max
[3,2,1,0,4]
 3 3 3 3 
```

## faster than 100%

```java
class Solution {
    public boolean canJump(int[] nums) {
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i > max) return false;
            if (max >= nums.length - 1) return true; // 達到終點提早返回結果
            max = Math.max(max, i + nums[i]);
        }
        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://timmybeeflin.gitbook.io/cracking-leetcode/greedy/55.-jump-game.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.
