赞
踩
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# 波谷和波峰的差。后一个减前一个,如果大于0,就加入利润
max_profit = 0
for i in range(1, len(prices)):
if prices[i] - prices[i-1] > 0:
max_profit += prices[i] - prices[i-1]
return max_profit
class Solution:
def canJump(self, nums: List[int]) -> bool:
max_index = 0
for i in range(len(nums)):
if max_index >= len(nums) - 1: return True
if max_index >= i:
max_index = max(i+nums[i], max_index)
else:
return False
return True
class Solution: def jump(self, nums: List[int]) -> int: if len(nums) == 1: return 0 cur_distance = 0 # 当前覆盖最远距离下标 ans = 0 # 记录走的最大步数 next_distance = 0 # 下一步覆盖最远距离下标 # 需要计算最小跳跃次数。贪心的话,依然是每次选最大的。 for i in range(len(nums)): next_distance = max(i+nums[i], next_distance) if i == cur_distance: ans += 1 cur_distance = next_distance if next_distance >= len(nums) -1: return ans
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。