leetcode 剑指 Offer 63. 股票的最大利润

   声明:本站部分内容来自互联网,如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

26

文章目录

    • 题目要求
    • 解题思路
      • 动态规划

题目要求

假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖该股票一次可能获得的最大利润是多少?

解题思路

动态规划

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