6.25 - hard - 11

45. Jump Game II
原理来挺简单的,就是把每一步可以达到的最远距离计算出来,有点像dynamic window的题目,只是left boundary是固定的 lastend + 1,right boundary是上一步能跳到的最大长度,不停的更新就可以了。

class Solution(object):
    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # get maximum jump in each jump
        if not nums or len(nums) == 1:
            return 0
        start = 0
        end = start + nums[0]
        count = 1
        while end < len(nums)-1:
            new_end = end
            for i in range(start, end+1):
                new_end = max(new_end, i + nums[i])
            
            start = end + 1
            end = new_end
            count += 1
        
        return count
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容