设为首页 加入收藏

TOP

45. Jump Game II Leetcode Python
2015-07-20 17:14:06 来源: 作者: 【 】 浏览:3
Tags:45. Jump Game Leetcode Python

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps.

For example:
Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)



This is also a greedy problem the difference between this one and the jump game is this time we need to count how many step we need to go from the start to the end.

we need to go through the whole array for once. The complexity if O(n).


we need two extra pointers "last" to track last element and count to track how many step we need to go.



code is as follow:

class Solution:
    # @param A, a list of integers
    # @return an integer
    def jump(self, A):
        index=0
        count=0
        last=0
        reach=0
        while reach>=index and index
  
   


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ 2318 TOYS 下一篇CodeForces 520D Cubes

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·【C语言】动态内存管 (2025-12-27 06:23:20)
·C语言中的内存管理 - (2025-12-27 06:23:16)
·C语言指南:C语言内 (2025-12-27 06:23:14)
·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)