121. Best Time to Buy and Sell Stock买卖股票12

Posted 排序和map

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了121. Best Time to Buy and Sell Stock买卖股票12相关的知识,希望对你有一定的参考价值。

[抄题]:

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Input: [7, 1, 5, 3, 6, 4]
Output: 5

max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

[一句话思路]:贪心算法。不要用数组i,j角标,直接用min, profit表示价格,更方便。

[画图]:

[一刷]:

min, profit都记得要初始化

[总结]:

[复杂度]:

n/1

[英文数据结构]:

[其他解法]:

[题目变变变]:

maximum subarray最大求和子数组

 

以上是关于121. Best Time to Buy and Sell Stock买卖股票12的主要内容,如果未能解决你的问题,请参考以下文章