jump_game

 1# @leet start
 2class Solution:
 3    def canJump(self, nums: list[int]) -> bool:
 4        """
 5        To figure out if we can reach the end of the game, we can iterate
 6        backwards.
 7        We then check if our index + the number can reach any further than
 8        our current best. If so, we update our position to the current index.
 9        At the end we make sure we can reach the start of the array, which
10        means we can make it from start to finish.
11        """
12        last_pos = len(nums) - 1
13
14        for i, num in reversed(list(enumerate(nums))):
15            if i + num >= last_pos:
16                last_pos = i
17
18        return last_pos == 0
19
20
21# @leet end
22
23
24def test():
25    assert 2 + 2 == 4
class Solution:
 3class Solution:
 4    def canJump(self, nums: list[int]) -> bool:
 5        """
 6        To figure out if we can reach the end of the game, we can iterate
 7        backwards.
 8        We then check if our index + the number can reach any further than
 9        our current best. If so, we update our position to the current index.
10        At the end we make sure we can reach the start of the array, which
11        means we can make it from start to finish.
12        """
13        last_pos = len(nums) - 1
14
15        for i, num in reversed(list(enumerate(nums))):
16            if i + num >= last_pos:
17                last_pos = i
18
19        return last_pos == 0
def canJump(self, nums: list[int]) -> bool:
 4    def canJump(self, nums: list[int]) -> bool:
 5        """
 6        To figure out if we can reach the end of the game, we can iterate
 7        backwards.
 8        We then check if our index + the number can reach any further than
 9        our current best. If so, we update our position to the current index.
10        At the end we make sure we can reach the start of the array, which
11        means we can make it from start to finish.
12        """
13        last_pos = len(nums) - 1
14
15        for i, num in reversed(list(enumerate(nums))):
16            if i + num >= last_pos:
17                last_pos = i
18
19        return last_pos == 0

To figure out if we can reach the end of the game, we can iterate backwards. We then check if our index + the number can reach any further than our current best. If so, we update our position to the current index. At the end we make sure we can reach the start of the array, which means we can make it from start to finish.

def test():
25def test():
26    assert 2 + 2 == 4