leetcode-121 买卖股票的最佳时机 Python
2020-03-15 12:14
656 查看
方法一:左到右
class Solution: def maxProfit(self, prices: List[int]) -> int: if not prices: return 0 res = 0 min_price = prices[0] for i in range(1, len(prices)): res = max(res, prices[i] - min_price) min_price = min(min_price, prices[i]) return res
方法二:右到左
class Solution: def maxProfit(self, prices: List[int]) -> int: if not prices: return 0 res = 0 max_price = prices[-1] for i in range(len(prices) - 2, -1, -1): res = max(res, max_price - prices[i]) max_price = max(max_price, prices[i]) return res
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- LeetCode 121 买卖股票的最佳时机 Python
- LeetCode || 121-124. 买卖股票的最佳时机
- 利用python 完成leetcode 122 买卖股票的最佳时机 II
- LeetCode 122 买卖股票的最佳时机2 Python
- LeetCode 121.买卖股票的最佳时机(一)
- leetcode-121-买卖股票的最佳时机
- Leetcode121: 买卖股票的最佳时机
- LeetCode题库解答与分析——#121. 买卖股票的最佳时机BestTimeToBuyAndSellStock
- LeetCode-121.买卖股票的最佳时机(相关话题:动态规划)
- 【leetcode系列】121. 买卖股票的最佳时机
- 利用python 完成 leetcode123 买卖股票的最佳时机 III
- LeetCode:买卖股票的最佳时机(Python版)
- C++Leetcode121:买卖股票的最佳时机
- LeetCode:买卖股票的最佳时机 II(Python版本)
- Leetcode之121. 买卖股票的最佳时机
- [leetcode] Python(2)--买卖股票的最佳时机 II(122)、旋转数组(189)
- 基础算法之买卖股票最佳时机(Leetcode-121)
- LeetCode刷题笔记121:买卖股票的最佳时机
- LeetCode 121. 买卖股票的最佳时机 python
- Leetcode121 Best Time to Buy and Sell Stock(买卖股票的最佳时机1)