[leetcode]Jump Game II @ Python

原题地址:https://oj.leetcode.com/problems/jump-game-ii/

题意:

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.)

解题思路:又是一个极其牛逼的解法,来自leetcode discuss。

代码:


class Solution:
# @param A, a list of integers
# @return an integer
# def jump(self, A):
# maxint = 1<<31 - 1
# dp = [ maxint for i in range(len(A)) ]
# dp[0] = 0
# for i in range(1, len(A)):
# for j in range(i):
# if A[j] >= (i - j):
# dp[i] = min(dp[i], dp[j] + 1)
# return dp[len(A) - 1]
# dp is time limited exceeded!

# We use "last" to keep track of the maximum distance that has been reached
# by using the minimum steps "ret", whereas "curr" is the maximum distance
# that can be reached by using "ret+1" steps. Thus,curr = max(i+A[i]) where 0 <= i <= last.
def jump(self, A):
ret = 0
last = 0
curr = 0
for i in range(len(A)):
if i > last:
last = curr
ret += 1
curr = max(curr, i+A[i])
return ret

[leetcode]Jump Game II @ Python

时间: 2024-12-26 12:46:11

[leetcode]Jump Game II @ Python的相关文章

[LeetCode] Jump Game II(贪婪算法)

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

LeetCode: Jump Game II [044]

Perface 如果让你实现这个页面和一些操作的,比如点击1.2.3等就在那个input text中显示,还有删除功能,拨打我们先不要管它,只是模拟而已.要是我刚开始做的话,我会这样做: 用css.HTML布局那个界面 用javascript的事件委托监听那个按钮的父节点的点击事件 但是如果我想用面向对象的思想做呢?我是用Ext做的,所以我想说的是它帮我封装了很多.可能一些没用过Ext的人不太了解我下面贴的代码,但是我会尽量解释清楚的! Description ContactTelPanel =

[leetcode]Word Ladder II @ Python

[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://blog.csdn.net/doc_sgl/article/details/13341405   http://chaoren.is-programmer.com/ 题意:给定start单词,end单词,以及一个dict字典.要求找出start到end的所有最短路径,路径上的每个单词都要出现在dict

[leetcode]Word Break II @ Python

原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "c

[leetcode]Unique Paths II @ Python

原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively i

LeetCode: Jump Game II 解题报告

Jump Game II 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 nu

leetcode Single Number II python

Single Number II Given an array of integers, every element appears three times except for one. Find that single one. python code: class Solution: # @param {integer[]} nums # @return {integer} def singleNumber(self, nums): B={} for eachint in nums: if

[LeetCode] Jump Game II 贪心

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

leetcode - Jump Game II

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