@@ -86,6 +86,58 @@ Output: 0
8686Explanation: In this case, no transaction is done, i.e. max profit = 0.
8787```
8888
89+ ## Solution
90+
91+ The solution uses a constant space, state-compressed dynamic programming approach to compute the maximum profit
92+ achievable, with at most 2 stock transactions in a single pass through the prices array. The updates are performed each
93+ day greedily, but the underlying logic is dynamic programming: each variable represents the best possible outcome up to
94+ the current point and depends on previously computed states. The core intuition is to model the problem as moving through
95+ four logical states: the cost of the first buy, the profit after the first sell, the effective cost of the second buy
96+ (after reinvesting the first profit), and the profit after the second sell. By continuously updating these values as we
97+ scan prices, we always keep track of the best possible outcome to date. The key insight is that the cost of the second
98+ buy can be reduced by the profit earned from the first transaction, allowing the two transactions to be combined optimally
99+ without overlap. This approach avoids the use of explicit dynamic programming tables and achieves optimal efficiency.
100+
101+ Now, let’s look at the steps of the solution:
102+
103+ 1 . We initialize four variables: t1Cost and t2Cost to infinity to represent the minimum cost of the first and second
104+ buys, and t1Profit and t2Profit to 0 to represent the maximum profit after the first and second sells.
105+ 2 . We iterate through each price[ i] in the prices array.
106+ - We update t1Cost to be the minimum of its current value and the current price, ensuring we always track the lowest
107+ price to buy the first stock.
108+ - We update t1Profit to be the maximum of its current value and the profit that would result from selling at the
109+ current price after buying at t1Cost.
110+ - Next, we update t2Cost to be the minimum of its current value and (price − t1Profit), which represents the
111+ effective cost of buying the second stock after reinvesting the profit from the first transaction.
112+ - We then update t2Profit to be the maximum of its current value and the profit that would result from selling at the
113+ current price after the second buy.
114+ 3 . After all prices have been processed, t2Profit contains the maximum profit achievable with at most two non-overlapping
115+ transactions, and we return this value.
116+
117+ ![ Solution 1] ( ./images/solutions/best_time_to_buy_and_sell_stock_3_solution_1.png )
118+ ![ Solution 2] ( ./images/solutions/best_time_to_buy_and_sell_stock_3_solution_2.png )
119+ ![ Solution 3] ( ./images/solutions/best_time_to_buy_and_sell_stock_3_solution_3.png )
120+ ![ Solution 4] ( ./images/solutions/best_time_to_buy_and_sell_stock_3_solution_4.png )
121+ ![ Solution 5] ( ./images/solutions/best_time_to_buy_and_sell_stock_3_solution_5.png )
122+ ![ Solution 6] ( ./images/solutions/best_time_to_buy_and_sell_stock_3_solution_6.png )
123+ ![ Solution 7] ( ./images/solutions/best_time_to_buy_and_sell_stock_3_solution_7.png )
124+ ![ Solution 8] ( ./images/solutions/best_time_to_buy_and_sell_stock_3_solution_8.png )
125+ ![ Solution 9] ( ./images/solutions/best_time_to_buy_and_sell_stock_3_solution_9.png )
126+ ![ Solution 10] ( ./images/solutions/best_time_to_buy_and_sell_stock_3_solution_10.png )
127+
128+ ### Time Complexity
129+
130+ The time complexity of this solution is O(n), where n is the length of the input array prices. This is because the
131+ algorithm makes a single pass through the array and, for each price, performs a constant number of arithmetic operations
132+ and comparisons to update the four tracking variables. As no nested loops or additional passes are involved, the total
133+ running time grows linearly with the number of days.
134+
135+ ### Space Complexity
136+
137+ The space complexity of this solution is O(1). The solution uses a fixed number of variables regardless of the input
138+ size. No additional data structures proportional to the input size are required, so the extra space used remains
139+ constant even as the input array grows.
140+
89141---
90142
91143# Best Time To Buy and Sell Stock With Transaction Fee
0 commit comments