imashutosh51

Jump Game ||

Jan 14th, 2026
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. class Solution:
  2.     def jump(self, arr: List[int]) -> int:
  3.         # If only one element, already at the end
  4.         if len(arr) == 1 or arr[0] == 0:
  5.             return 0
  6.  
  7.         l = 0              # left boundary of current jump range
  8.         r = 0              # right boundary of current jump range
  9.         jumps = 0
  10.  
  11.         # Continue until the last index is reachable
  12.         while r < len(arr) - 1:
  13.             farthest = r   # farthest index reachable in the next jump
  14.  
  15.             # Explore all indices in the current range [l, r]
  16.             for i in range(l, r + 1):
  17.                 # From index i, try to extend the reach
  18.                 farthest = max(farthest, i + arr[i])
  19.  
  20.             # Move to the next jump range
  21.             l = r + 1
  22.             r = farthest
  23.  
  24.             # One jump completed
  25.             jumps += 1
  26.  
  27.         return jumps
  28.  
Advertisement
Add Comment
Please, Sign In to add comment