LeetCode 45. Jump Game II

简介: 给定一个非负整数数组,初始位置在索引为0的位置,数组中的每个元素表示该位置的能够跳转的最大部署。目标是以最小跳跃次数到达最后一个位置(索引)。

Description



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.


Example: Input: [2,3,1,1,4] Output: 2


Explanation: 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.

Note:You can assume that you can always reach the last index.


描述



给定一个非负整数数组,初始位置在索引为0的位置,数组中的每个元素表示该位置的能够跳转的最大部署。目标是以最小跳跃次数到达最后一个位置(索引)。


例:输入[2,3,1,1,4],输出 2


解释,初始位置为0(此时,0最多能够往后跳两次),0跳到索引为1的位置(此时最多能够往后面跳3次),然后跳到末尾


思路



贪心算法


v2-b0cac0b0efde605ca11825ac04cb9a6c_720w.jpg


  • 如上图所示,初始位置0,能往后走4步,最远到达A[4]
  • 于是我们遍历从A[1]到A[4]的位置
  • A[1]=1,能往后走一步,到达A[2]
  • A[2]=1,能往后走一步,到达A[3]
  • A[3]=1,能往后走一步,到达A[4]
  • A[4]=3,能往后走一步,到达A[7]
  • 我们发现到达A[4],下一步到达最远,于是我们选择从A[0]跳到A[4]
  • 注意:如果遍历发现A[1]到A[4]中最远也只能到达A[4],则我们直接让A[0]跳到A[4],不通过A[1]到A[4]之间的点跳转,减少次数


class Solution:
    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # 记录步数
        count = 0
        # 下标
        index, length = 0, len(nums)
        # 只有第一个到倒数第二个之间才需要走,最后一个已经到达目的地,不需要在往下走
        while index < length-1:
            # 如果当前位置能直接走到最后一个位置,说明再走一次就可以到达尾部,返回count+1
            if index + nums[index] >= length-1:
                return count + 1
            # 寻找从index+1位置开始,到index + nums[index],依次寻找能跳到最远位置的值
            # 能跳到最远位置的值,初始化为index
            maxindex = index
            # 最远的位置,初始化为index当前的位置加上当前位置能够跳的最远值
            temp = nums[index]+index
            # 依次遍历寻找
            for subindex in range(index+1, index+nums[index]+1):
                # 注意,这里不能取等号,因为如果能够从index位置一次跳到末尾,就不再需要中间多停留一次
                if nums[subindex]+subindex > temp:
                    temp = nums[subindex]+subindex
                    maxindex = subindex
            # 找到最大值,index跳到对应位置
            # 如果index没有变动
            if index == maxindex:
                index = nums[index]+index
            else:
                # 如果index变动
                index = maxindex
            # 步数记录自增一次
            count += 1
        return count
if __name__ == "__main__":
    so = Solution()
    res = so.jump([4, 1, 1, 3, 1, 1, 1])
    print(res)


源代码文件在这里.

目录
相关文章
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode 403. Frog Jump
一只青蛙想要过河。 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有)。 青蛙可以跳上石头,但是不可以跳入水中。 给定石子的位置列表(用单元格序号升序表示), 请判定青蛙能否成功过河(即能否在最后一步跳至最后一个石子上)。 开始时, 青蛙默认已站在第一个石子上,并可以假定它第一步只能跳跃一个单位(即只能从单元格1跳至单元格2)。 如果青蛙上一步跳跃了 k 个单位,那么它接下来的跳跃距离只能选择为 k - 1、k 或 k + 1个单位。 另请注意,青蛙只能向前方(终点的方向)跳跃。
79 0
LeetCode 403. Frog Jump
LeetCode 390. Elimination Game
给定一个从1 到 n 排序的整数列表。 首先,从左到右,从第一个数字开始,每隔一个数字进行删除,直到列表的末尾。 第二步,在剩下的数字中,从右到左,从倒数第一个数字开始,每隔一个数字进行删除,直到列表开头。 我们不断重复这两步,从左到右和从右到左交替进行,直到只剩下一个数字。 返回长度为 n 的列表中,最后剩下的数字。
73 0
LeetCode 390. Elimination Game
LeetCode 292. Nim Game
你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。你作为先手。 你们是聪明人,每一步都是最优解。 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。
55 0
LeetCode 292. Nim Game
|
存储 算法
LeetCode 289. Game of Life
如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡; 如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活; 如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡; 如果死细胞周围正好有三个活细胞,则该位置死细胞复活;
49 0
LeetCode 289. Game of Life
|
算法 索引
LeetCode 55. Jump Game
给定一个非负整数数组,您最初定位在数组的第一个索引处。 数组中的每个元素表示该位置的最大跳转长度。 确定您是否能够到达最后一个索引。
72 0
LeetCode 55. Jump Game
|
决策智能
LeetCode之Nim Game
LeetCode之Nim Game
102 0
|
C++
LeetCode 289 Game of Life(生命游戏)(Array)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/52122735 翻译 根据维基百科的文章介绍:“Game of Life,简称为Life,是一个被英国数学家John Conway在1970年提出的细胞自动分裂器。
1608 0
|
2月前
|
机器学习/深度学习 算法
力扣刷题日常(一)
力扣刷题日常(一)
20 2

热门文章

最新文章