← Home

123. Best Time to Buy and Sell Stock III

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
123

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 123. Best Time to Buy and Sell Stock III, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

The notes already sitting in the source point us in the right direction:

  • https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/39611/Is-it-Best-Solution-with-O(n)-O(1).
  • time: O(N), space: O(1)

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are maxProfit.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(N), space: O(1)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/39611/Is-it-Best-Solution-with-O(n)-O(1).
02//Runtime: 8 ms, faster than 68.97% of C++ online submissions for Best Time to Buy and Sell Stock III.
03//Memory Usage: 7.3 MB, less than 100.00% of C++ online submissions for Best Time to Buy and Sell Stock III.
04//time: O(N), space: O(1)
05class Solution {
06public:
07    int maxProfit(vector<int>& prices) {
08        //the cash in hand when we have bought 1 or 2 stocks
09        /*
10        we need to initialize hold1 and hold2 to INT_MIN, 
11        because they are required to compared with "-price"
12        */
13        int hold1 = INT_MIN, hold2 = INT_MIN;
14        //the cash in hand when we have sold 1 or 2 stocks
15        /*
16        the minimum of release1 and release2 is 0, 
17        no need to be INT_MIN
18        */
19        int release1 = 0, release2 = 0;
20        
21        for(int price : prices){
22            //their values depend on the value in previous iteration, so their order is required to be like this
23            //release2 is always >= release1
24            release2 = max(release2, hold2+price);
25            //hold2 is always >= hold1
26            hold2 = max(hold2, release1-price);
27            //release1 is always >= 0
28            release1 = max(release1, price+hold1);
29            hold1 = max(hold1, -price);
30        }
31        
32        //release2 is always max(release1, release2)
33        return release2;
34    }
35};

Cost

Complexity

Time
O(N), space: O(1)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.