121 Best Time to Buy and Sell Stock
1. Question
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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
Example 1:
Example 2:
2. Implementation
(1) Greedy
3. Time & Space Complexity
时间复杂度O(n), 空间复杂度O(1)
Last updated