Best Time to Buy and Sell Stock III - Complete Solution Guide
Best Time to Buy and Sell Stock III is LeetCode problem 123, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an array prices where prices[i] is the price of a given stock on the i th day. Find the maximum profit you can achieve. You may complete at most two transactions . Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again). Example 1: Input: prices = [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. Then buy on day 7 (price = 1) and sell on day 8 (price = 4),
Detailed Explanation
The problem asks us to find the maximum profit achievable by buying and selling a stock at most two times, given an array of prices where `prices[i]` is the price of the stock on the i-th day. A crucial constraint is that you cannot hold two stocks simultaneously, meaning you must sell before buying again. The input is the `prices` array, and the output is the maximum profit.
Solution Approach
The solution utilizes a dynamic programming approach with constant space. It iterates through the `prices` array and updates four variables: `buy1`, `sell1`, `buy2`, and `sell2`. `buy1` stores the minimum cost of buying the first stock. `sell1` stores the maximum profit after the first transaction. `buy2` stores the minimum cost of buying the second stock after the first transaction. `sell2` stores the maximum profit after the second transaction. By iterating through the prices and updating these variables appropriately, the final `sell2` will hold the maximum profit that can be achieved with at most two transactions.
Step-by-Step Algorithm
- Step 1: Initialize `buy1` and `buy2` to negative infinity (or INT_MIN) because we want to minimize the cost when we buy stock, starting with the lowest possible value. Initialize `sell1` and `sell2` to 0 because initially, we have no profit.
- Step 2: Iterate through the `prices` array.
- Step 3: In each iteration, update `sell2` as the maximum of its current value and `buy2 + price` (selling the second stock at the current price).
- Step 4: Update `buy2` as the maximum of its current value and `sell1 - price` (buying the second stock at the current price after making profit `sell1` from the first transaction).
- Step 5: Update `sell1` as the maximum of its current value and `buy1 + price` (selling the first stock at the current price).
- Step 6: Update `buy1` as the maximum of its current value and `-price` (buying the first stock at the current price. Initial investment will be negative).
- Step 7: After iterating through all prices, return `sell2`, which represents the maximum profit achievable with at most two transactions.
Key Insights
- Insight 1: We can think of the problem as finding the best split point in the `prices` array and maximizing profit from the left and right subarrays independently. However, iterating through all split points would be O(n^2).
- Insight 2: The optimal solution avoids explicitly splitting the array. Instead, it uses dynamic programming with constant space to track the maximum profit after at most two transactions by maintaining four variables: buy1, sell1, buy2, and sell2, representing buying the first stock, selling the first stock, buying the second stock, and selling the second stock respectively.
- Insight 3: The key to the O(n) solution lies in updating these four variables in the correct order in each iteration. The `buy1` and `sell1` always represents at most one transaction and `buy2` and `sell2` represents at most two transactions.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Dynamic Programming.
Companies
Asked at: Bolt, Citadel, Groww, PhonePe, Snap, Tekion, TikTok, Uber, Visa.