← Home

1422. Maximum Score After Splitting a String

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
two pointersC++Markdown
142

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1422. Maximum Score After Splitting a String, the solution in this repository is mainly a two pointers solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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: two pointers.

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 maxScore, left.

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. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

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

solution.cpp
01//Runtime: 16 ms, faster than 14.29% of C++ online submissions for Maximum Score After Splitting a String.
02//Memory Usage: 6.9 MB, less than 100.00% of C++ online submissions for Maximum Score After Splitting a String.
03class Solution {
04public:
05    int maxScore(string s) {
06        int ans = 0;
07        int n = s.size();
08        vector<int> left(n, 0), right(n, 0);
09        
10        for(int i = 0; i < s.size(); i++){
11            left[i] = ((i > 0) ? left[i-1] : 0) + (s[i] == '0');
12            right[s.size()-1-i] = ((i > 0) ? right[s.size()-1-i+1] : 0) + (s[s.size()-1-i] == '1');
13        }
14        
15        for(int i = 0; i < n; i++){
16            cout << left[i] << " ";
17        }
18        cout << endl;
19        
20        for(int i = 0; i < n; i++){
21            cout << right[i] << " ";
22        }
23        cout << endl;
24        
25        for(int i = 0; i < n-1; i++){
26            ans = max(ans, left[i] + right[i+1]);
27        }
28        
29        return ans;
30    }
31};
32
33//One Pass
34//https://leetcode.com/problems/maximum-score-after-splitting-a-string/discuss/597716/Java-5-Liner-(One-Pass)
35//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Maximum Score After Splitting a String.
36//Memory Usage: 6.5 MB, less than 100.00% of C++ online submissions for Maximum Score After Splitting a String.
37class Solution {
38public:
39    int maxScore(string s) {
40        int zeros = 0, ones = 0, ans = INT_MIN;
41        int n = s.size();
42        /*
43        ans 
44        = max(lZero+rOne)
45        = max(lZero+totalOne-lOne)
46        = max(lZero-lOne) + totalOne
47        */
48        for(int i = 0; i < n; i++){
49            if(s[i] == '0') zeros++;
50            else ones++;
51            /*
52            there should be at least one char in the right part,
53            so don't update ans in the last iteration
54            */
55            if(i != n-1) ans = max(ans, zeros-ones);
56        }
57        
58        return ans + ones;
59    }
60};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
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.