文章目录
- 题目要求
- 解题思路
- 动态规划
题目要求
假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖该股票一次可能获得的最大利润是多少?
解题思路
动态规划
class Solution(object):def maxProfit(self, prices):""":type prices: List[int]:rtype: int"""if not prices:return 0min_price = prices[0]max_profit = 0for i in range(len(prices)):min_price = min(min_price, prices[i])cur_profit = prices[i] - min_pricemax_profit = max(max_profit, cur_profit)return max_profit










