121. 买卖股票的最佳时机
2019-04-04 20:06
148 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Hey1213/article/details/89034592
本题主要是是求序列中两个数字的最大差,其中第一个数必须在第二个数前面
O(n)就可以解决问题
class Solution: def maxProfit(self, prices: List[int]) -> int: max_profit = 0 min_index = 0 for i in range(len(prices)): if prices[i] - prices[min_index] > max_profit: max_profit = prices[i] - prices[min_index] elif prices[i] < prices[min_index]: min_index = i return max_profit
相关文章推荐
- Leetcode121: 买卖股票的最佳时机
- C++Leetcode121:买卖股票的最佳时机
- 基础算法之买卖股票最佳时机(Leetcode-121)
- Leetcode121 Best Time to Buy and Sell Stock(买卖股票的最佳时机1)
- LeetCode题库解答与分析——#121. 买卖股票的最佳时机BestTimeToBuyAndSellStock
- 121. 买卖股票的最佳时机
- LeetCode 121 买卖股票的最佳时机 Python
- LeetCode-121.买卖股票的最佳时机(相关话题:动态规划)
- [Leetcode] Best time to buy and sell stock 买卖股票的最佳时机
- [Leetcode]2018.3.22买卖股票的最佳时机 II
- 买卖股票的最佳时机
- Leetcode 121. 买卖股票的最佳时机
- 【刷算法】LeetCode.150-买卖股票的最佳时机 II
- LeetCode-123.买卖股票的最佳时机III(相关话题:动态规划)
- 买卖股票的最佳时机 II
- leetcode 121. 买卖股票的最佳时机
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- 买卖股票的最佳时机 —LintCode
- 买卖股票的最佳时机
- LintCode-买卖股票的最佳时机