题目
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。
注意你不能在买入股票前卖出股票。
示例 1:
输入: [7,1,5,3,6,4]
输出: 5
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。
示例 2:
输入: [7,6,4,3,1]
输出: 0
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路
解法一:暴力算法
暴力算法不用多说,就是数组大就会超时
class Solution:
def maxProfit(self, prices: List[int]) -> int:
maxprofit = 0
for i in range(0,len(prices)):
for j in range(i+1,len(prices) ):
profit = prices[j] - prices[i]
if (profit > maxprofit):
maxprofit = profit
return maxprofit
解法二.DP
参考最大子序和
把最大子序和改写成两元素的差值,
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if len(prices) == 0:
return 0
p = prices[0]
maxp = 0
curr_max = 0
for a in prices[1:]:
x = a - p
p = a
curr_max = curr_max + x if curr_max > 0 else x
#记录当前元素与之前元素的最大差值
maxp = max(maxp, curr_max)
#记录全局最大差值
return maxp