Maximum Profit from Selling Before Buying
Problem statement
Given daily stock prices prices, you may make at most one transaction: sell one unit on day i, then buy it back on a strictly later day j.
The profit is prices[i] - prices[j]. Return the maximum profit, or 0 if making no transaction is better. There are no fees or other costs.
Function
maximumShortProfit(prices: int[]) → intExamples
Example 1
prices = [8,4,6,2]return = 6Sell at 8 on day 0 and buy at 2 on day 3, for a profit of 6.
Example 2
prices = [1,2,3]return = 0Every later buy costs more than the earlier sale. Making no transaction yields 0.
Constraints
0 <= prices.length <= 100000.0 <= prices[i] <= 1000000000.