LeetCode 55. Jump Game

简介: 给定一个非负整数数组,您最初定位在数组的第一个索引处。数组中的每个元素表示该位置的最大跳转长度。确定您是否能够到达最后一个索引。

v2-2ef9c9701b29e2ce59c71c31c76767d1_1440w.jpg

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.

Determine if you are able to reach the last index.


Example 1:


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

Output: true

Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.


Example 2:


Input: [3,2,1,0,4]

Output: false

Explanation: You will always arrive at index 3 no matter what. Its maximum

jump length is 0, which makes it impossible to reach the last index.


描述



给定一个非负整数数组,您最初定位在数组的第一个索引处。

数组中的每个元素表示该位置的最大跳转长度。

确定您是否能够到达最后一个索引。


思路



这道题和第46题属于同一题目,这道题更简单一点,只是问能否到达末尾。第46题在这里,解题在这里.


  • 我们声明一个变量maxindex表示当前能够走到的最大索引位置
  • 我们依次检查每一个位置能够走到的最大值,如果能够走到当前位置且当前位置能够走到的最远位置大于maxindex,我们就更新maxindex
  • 一旦maxindx大于等于索引最大值,我们返回True,否则我们一直遍历到末尾,如果此时maxindex还小于索引最大值,我们就返回False
  • 本题的基本思路也是贪心算法


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2018-12-15 17:13:28
# @Last Modified by:   何睿
# @Last Modified time: 2018-12-15 17:19:47
class Solution:
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        # 记录最远位置
        maxindex = 0
        for i in range(len(nums)):
            # 如果能偶到达当前位置并且当前位置能够到达的最远位置大于maxindex
            if maxindex >= i and i+nums[i] > maxindex:
                # 更新maxindex
                maxindex = i+nums[i]
            # 一旦maxindex大于等于最大索引,我们返回False
            if maxindex >= len(nums)-1:
                return True
        # 如果到末尾maxinde都小于最大索引,返回False
        return False
if __name__ == "__main__":
    so = Solution()
    res = so.canJump([3, 2, 1, 0, 4])
    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 45. Jump Game II
给定一个非负整数数组,初始位置在索引为0的位置,数组中的每个元素表示该位置的能够跳转的最大部署。目标是以最小跳跃次数到达最后一个位置(索引)。
50 0
LeetCode 45. Jump Game II
|
决策智能
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

热门文章

最新文章