Greedy Algorithms8 sections · 316 units
Open in Course

Jump Game II - Implementation

The code

function jump(nums)
 jumps := 0
 currentEnd := 0
 furthest := 0
 for i from 0 to length of nums - 2
 furthest := max(furthest, i + nums[i])
 if i = currentEnd then
 jumps := jumps + 1
 currentEnd := furthest
 return jumps

Time: O(n)O(n). Space: O(1)O(1).