L149 Best Time to Buy and Sell Stock
Say you have an array for which thei_thelement is the price of a given stock on day_i.
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.
Example
Given array[3,2,3,1,2]
, return1
.
因为只能做一次交易,所以一边loop一边找min(买入点)。用现在的价钱-min 算一个差值(表示今天卖出),max这个差值就是结果。T:O(n),S:O(1)
Previous714 Best Time to Buy and Sell Stock with Transaction FeeNextL150 Best Time to Buy and Sell Stock II
Last updated