0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Leetcode #55: Jump Game

Posted at

We will stop only if the jump length from the current position is zero and the current position is the furthest we can jump from any preceding position.

func canJump(_ nums: [Int]) -> Bool {
    if nums.isEmpty {
        return false
    }
    var maxReach = 0
    for i in 0..<nums.count - 1 {
        if maxReach < nums[i] + i {
            maxReach = nums[i] + i
        }

        if nums[i] == 0 && maxReach == i {
            return false;
        }
    }
    return true
}
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?