Sunday, August 16, 2015

Leetcode 45. Jump Game II

https://leetcode.com/problems/jump-game-ii/

Have no idea why ii is before i. The solution is to record the max range that one jump can reach and update the bound.

Solution:
# T:O(n) S:O(1)
class Solution:
    # @param {integer[]} nums
    # @return {integer}
    def jump(self, nums):
        last = curr = ret = 0
        for i in xrange(len(nums)):
            if i > last:
                last = curr
                ret += 1
            curr = max(curr, nums[i]+i)
        return ret
Run Time: 68 ms

No comments:

Post a Comment