Practice/Oracle/Leetcode 123. Best Time to Buy and Sell Stock III
CodingOptional
You are given an array where each element represents the price of a stock on a particular day. Your goal is to determine the maximum profit you can achieve by completing at most two separate buy-sell transactions.
Important rules:
Return the maximum profit that can be obtained.
Example 1:
Input: prices = [3, 3, 5, 0, 0, 3, 1, 4] Output: 6 Explanation: Purchase on day 4 (price = 0), sell on day 6 (price = 3), profit = 3. Then purchase on day 7 (price = 1), sell on day 8 (price = 4), profit = 3. Total profit = 3 + 3 = 6.
Example 2:
Input: prices = [1, 2, 3, 4, 5] Output: 4 Explanation: Purchase on day 1 (price = 1) and sell on day 5 (price = 5). Profit = 4. A second transaction isn't beneficial.
Example 3:
Input: prices = [7, 6, 4, 3, 1] Output: 0 Explanation: Prices only decrease, so no profitable transaction exists.
Example 4:
Input: prices = [1, 4, 2, 7, 3, 9] Output: 12 Explanation: Purchase on day 1 (price = 1), sell on day 4 (price = 7), profit = 6. Then purchase on day 5 (price = 3), sell on day 6 (price = 9), profit = 6. Total profit = 6 + 6 = 12.