πŸ“ Problem Details

πŸ’­What Were My Initial Thoughts?

- my intuition for this problem was incorrect / bad so i will be omitting it here 

πŸ’‘ Explanation of Solution

1. track frequency of zeros and ones

2. optimal split calculation (best)
	- the variable `best` keeps track of the maximum difference between zeros and ones up to any split point
	- this difference helps maximise the score after adding the count of 1s to the right part 

3. final adjustment (best + ones)
	- after completing the loop iteration, best represents the optimal score contribution from the left part
	- to compute the final score, we add all remaining 1s from the right part, to best

βŒ› Complexity Analysis

Time Complexity: O(n)
Space Complexity: O(1)

πŸ’» Implementation of Solution

class Solution {
public:
Β  Β  int maxScore(string s) {
Β  Β  Β  Β  int ones = 0;
Β  Β  Β  Β  int zeros = 0;
Β  Β  Β  Β  int best = INT_MIN;
  
Β  Β  Β  Β  for(int i=0; i < s.size()-1; i++){
Β  Β  Β  Β  Β  Β  if(s[i] == '1') {
Β  Β  Β  Β  Β  Β  Β  Β  ones++;
Β  Β  Β  Β  Β  Β  }
Β  Β  Β  Β  Β  Β  else {
Β  Β  Β  Β  Β  Β  Β  Β  zeros++;
Β  Β  Β  Β  Β  Β  }
Β  Β  Β  Β  Β  Β  best = max(best, zeros - ones);
Β  Β  Β  Β  }
 
Β  Β  Β  Β  if( s[s.size() -1 ] == '1') {
Β  Β  Β  Β  Β  Β  ones++;
Β  Β  Β  Β  }
Β  Β  Β  Β  return best + ones;
Β  Β  }
};