数组 prices 记录了某芯片近期的交易价格,其中 prices[i] 表示的 i 天该芯片的价格。你只能选择 某一天 买入芯片,并选择在 未来的某一个不同的日子 卖出该芯片。请设计一个算法计算并返回你从这笔交易中能获取的最大利润。

如果你不能获取任何利润,返回 0。

示例 1:

**输入:**prices = [3, 6, 2, 9, 8, 5]
**输出:**7
**解释:**在第 3 天(芯片价格 = 2)买入,在第 4 天(芯片价格 = 9)卖出,最大利润 = 9 - 2 = 7。

示例 2:

**输入:**prices = [8, 12, 15, 7, 3, 10]
**输出:**7
**解释:**在第 5 天(芯片价格 = 3)买入,在第 6 天(芯片价格 = 10)卖出,最大利润 = 10 - 3 = 7。

提示:

0 <= prices.length <= 10^5

0 <= prices[i] <= 10^4

买入的天数为buy,卖出的天数为sell,一定有sell>buy

找到最大的price[sell]-prive[buy]

price[buy]一定是sell之前的最小值,维护一个buy,我们就可以遍历price中每个值分别作为sell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int bestTiming(vector<int> &prices) {
if (prices.empty())
return 0;
int buy = prices[0];
int ans = 0;
for (int i : prices) {
if (i < buy)
buy = i;
ans = max(ans, i - buy);
}
return ans;
}
};