博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
阅读量:5294 次
发布时间:2019-06-14

本文共 1205 字,大约阅读时间需要 4 分钟。

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: trueExplanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: [3,2,1,0,4]Output: falseExplanation: 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. 这个题目因为是问的yes/no,然后跟坐标有关(也就是说如果数组里面有数字互相调换了答案就不一样了),很有可能是dynamic programming,这里用动态规划来做。f(i) 表示能否跳到该点,function 用 f(i) = true if f(j) and nums[j] >= i - j (for loop), 一旦有就break,这里可以稍微improve一点时间上的效率。 T: O(n^2)   S: O(n) note:但是在leetcode上面这个会limit time exceed。 Code:
class Solution:    def jumpGame(self, nums):        if not nums: return False        n = len(nums)        mem = [False] * n        mem[0] = True        for i in range(1, n):            for j in range(0, i):                if mem[j] and nums[j] >= i - j:                    mem[i] = True                    break        return mem[n - 1]

 

转载于:https://www.cnblogs.com/Johnsonxiong/p/10760968.html

你可能感兴趣的文章
Selenium_Python接口-Alert类
查看>>
linux远程win7教程
查看>>
移动应用开发选型:向左还是向右?
查看>>
开发进度一
查看>>
十天冲刺(6)
查看>>
加载selenium2Library失败---robotframework环境搭建(site-packages下无selenium2library文件夹)...
查看>>
MyBaits学习
查看>>
实体标签,媒体标签,飘动标签
查看>>
MySQL安装的详细步骤
查看>>
Deformity JSP Webshell、Webshell Hidden Learning
查看>>
管道,数据共享,进程池
查看>>
Java基础--面向对象编程4(多态)
查看>>
CSS
查看>>
shell 管道和tee使用时获取前面命令返回值
查看>>
[LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
查看>>
[Cypress] Stub a Post Request for Successful Form Submission with Cypress
查看>>
[TypeScript] Understanding Generics with RxJS
查看>>
一、基础篇--1.3进程和线程-基本概念
查看>>
Linux kernel ‘ioapic_read_indirect’函数拒绝服务漏洞
查看>>
WordPress GRAND FlAGallery插件“s”跨站脚本漏洞
查看>>