[闲聊] LeetCode 121

楼主: sustainer123 (caster)   2023-01-12 23:58:52
121. Best Time to Buy and Sell Stock
给你一串股票价格数列,你想要获得最大利益,请选择一天买入,
然后在未来的任一天卖出,如有收益,则回传收益,如无利可图,请回传0。
Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit =
6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you
must buy before you sell.
Example 2:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
思路:
满直观的题目,先默认最小值为阵列第一个数字,如果prices[i]<min,
则min = price[i]。
或者pricse[i]-min > max,则maxProfit=prices[i]-min。
C Code

Links booklink

Contact Us: admin [ a t ] ucptt.com