I like to read this solution as a small machine: keep the useful information, throw away the noise. For 152. Maximum Product Subarray, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. 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: dynamic programming.
The notes already sitting in the source point us in the right direction:
- TLE
- 183 / 184 test cases passed.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are maxProduct.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- 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:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
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) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//TLE
02//183 / 184 test cases passed.
03class Solution {
04public:
05 int maxProduct(vector<int>& nums) {
06 int n = nums.size();
07 vector<vector<int>> dp(n, vector(n, 1));
08 int ans = INT_MIN;
09
10 for(int i = n-1; i >= 0; i--){
11 for(int j = i; j < n; j++){
12 //[i...j]
13 // cout << i << ", " << j << endl;
14 if(j == i){
15 //interval of length 1
16 dp[i][j] = nums[i];
17 }else{
18 dp[i][j] = nums[i] * dp[i+1][j];
19 }
20 ans = max(ans, dp[i][j]);
21 }
22 }
23
24 return ans;
25 }
26};
27
28//O(n) DP
29//https://leetcode.com/problems/maximum-product-subarray/discuss/48230/Possibly-simplest-solution-with-O(n)-time-complexity
30//Runtime: 8 ms, faster than 40.36% of C++ online submissions for Maximum Product Subarray.
31//Memory Usage: 11.8 MB, less than 5.00% of C++ online submissions for Maximum Product Subarray.
32class Solution {
33public:
34 int maxProduct(vector<int>& nums) {
35 int n = nums.size();
36 int ans = nums[0];
37 int curmax = nums[0], curmin = nums[0];
38
39 for(int i = 1; i < n; i++){
40 /*
41 multiplied by a negative makes big number smaller,
42 small number bigger
43 so we redefine the extremums by swapping them
44 */
45 if(nums[i] < 0){
46 swap(curmax, curmin);
47 }
48
49 /*
50 note the first item of max() is nums[i], not curmax!!
51 it either extends the previous found subarray or
52 create a new subarray
53 */
54 curmax = max(nums[i], curmax*nums[i]);
55 curmin = min(nums[i], curmin*nums[i]);
56
57 ans = max(curmax, ans);
58 }
59
60 return ans;
61 }
62};
Cost