122. Best Time to Buy and Sell Stock II
You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.

Why Greed? the final profit is breakable
Time complexity: O(n)
Space complexity: O(1)
greed:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
result = 0
for i in range(1, len(prices)):
if prices[i] - prices[i-1] > 0:
result += prices[i] - prices[i-1]
return result
Dynamic programming:
Time complexity: O(n)
Space complexity O(n)
class Solution:
def maxProfit(self, prices: List[int]) -> int:
length = len(prices)
dp = [[0] * 2 for _ in range(length)]
dp[0][0] = -prices[0]
dp[0][1] = 0
for i in range(1, length):
dp[i][0] = max(dp[i-1][0], dp[i-1][1] - prices[i]) #注意这里是和121. 买卖股票的最佳时机唯一不同的地方
dp[i][1] = max(dp[i-1][1], dp[i-1][0] + prices[i])
return dp[-1][1]
You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.

each element in the array represents your maximum jump length .
jump length <= nums[i] not jump lenth ==nums[i]
greedy
Time complexity: O(n)
Space complexity: O(1)
back-to-front jump:
class Solution:
def canJump(self, nums: List[int]) -> bool:
cover = 0
if len(nums) == 1: return True
i = 0
# python不支持动态修改for循环中变量,使用while循环代替
while i <= cover:
cover = max(i + nums[i], cover)
if cover >= len(nums) - 1: return True
i += 1
return False
front-to-back jump:
class Solution:
def canJump(self, nums: List[int]) -> bool:
last = len(nums) - 1
for i in range(last, -1, -1):
if nums[i] != 0 and nums[i] >= last - i:
last = i
if last == 0:
return True
return False
You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].
Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:
0 <= j <= nums[i]andi + j < n
Return the minimum number of jumps to reach nums[n - 1].
The test cases are generated such that you can reach nums[n - 1].

range(0): does not contain any integer, whose length is zero.
greedy 1:
Time complexity: O(n)
Space complexity: O(1)

There is still a special condition to consider here:
If the furthest distance index is the end of the set, the number of steps does not need to be increased by one, because you can't go back any further.
class Solution:
def jump(self, nums):
if len(nums) == 1:
return 0
cur_distance = 0 # 当前覆盖最远距离下标
ans = 0 # 记录走的最大步数
next_distance = 0 # 下一步覆盖最远距离下标
for i in range(len(nums)):
next_distance = max(nums[i] + i, next_distance) # 更新下一步覆盖最远距离下标
if i == cur_distance: # 遇到当前覆盖最远距离下标
ans += 1 # 需要走下一步
cur_distance = next_distance # 更新当前覆盖最远距离下标(相当于加油了)
if next_distance >= len(nums) - 1: # 当前覆盖最远距离达到数组末尾,不用再做ans++操作,直接结束
break
return ans
(greedy 2):
regardless of whether it's the end of the line or not.
Because the last step must be the end that can be reached. (The title assumes that the last position of the array is always reachable)The test cases are generated such that you can reach nums[n - 1].
class Solution:
def jump(self, nums):
cur_distance = 0 # 当前覆盖的最远距离下标
ans = 0 # 记录走的最大步数
next_distance = 0 # 下一步覆盖的最远距离下标
for i in range(len(nums) - 1): # 注意这里是小于len(nums) - 1,这是关键所在
next_distance = max(nums[i] + i, next_distance) # 更新下一步覆盖的最远距离下标
if i == cur_distance: # 遇到当前覆盖的最远距离下标
cur_distance = next_distance # 更新当前覆盖的最远距离下标
ans += 1
return ans
文章讨论了在股票价格变动和跳跃游戏中寻找最大利润的问题,分别介绍了使用贪婪算法和动态规划解决买卖股票的最佳时机以及跳跃游戏中的最少跳跃次数。两种方法都考虑了如何利用给定条件最大化收益或到达目标位置。
1378

被折叠的 条评论
为什么被折叠?



