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