Practice/Meta/Leetcode 121. Best Time to Buy and Sell Stock
CodingMust
You are given an array representing the daily prices of a stock over a period of time. Each element in the array corresponds to the stock's price on a specific day.
Your goal is to determine the maximum profit you can achieve by completing at most one transaction. A transaction consists of buying the stock on one day and selling it on a strictly later day.
If no profitable transaction is possible, return 0.
1 <= prices.length <= 100,0000 <= prices[i] <= 10,000Example 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 profitable transactions can be made, so the maximum profit is 0.
Example 3:
Input: prices = [2, 4, 1] Output: 2 Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4 - 2 = 2.