Problem solution · C++

Maximum Difference Between Even and Odd Frequency II

Maximum Difference Between Even and Odd Frequency II: a C++ solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sliding window or two pointers
Source
walkccc LeetCode Solutions
Length
40 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For Maximum Difference Between Even and Odd Frequency II, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 40 lines of C++ from the credited upstream file 3445.cpp.
  • The implementation visibly relies on sequence storage.
  • 5 loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMaximum Difference Between Even and Odd Frequency II · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  int maxDifference(string s, int k) {    int ans = INT_MIN;     for (const auto& [a, b] : getPermutations()) {      // minDiff[parityA][parityB] := min(a - b) of all valid windows with      // parityA and parityB      vector<vector<int>> minDiff(2, vector<int>(2, INT_MAX / 2));      vector<int> prefixA{0};  // prefixA[i] := the number of 'a's in s[0..i)      vector<int> prefixB{0};  // prefixB[i] := the number of 'b's in s[0..i)      for (int l = 0, r = 0; r < s.length(); ++r) {        prefixA.push_back(prefixA.back() + (s[r] == a ? 1 : 0));        prefixB.push_back(prefixB.back() + (s[r] == b ? 1 : 0));        while (r - l + 1 >= k &&               // the window size >= k               prefixA[l] < prefixA.back() &&  // the number of 'a's > 0               prefixB[l] < prefixB.back()) {  // the number of 'b's > 0          minDiff[prefixA[l] % 2][prefixB[l] % 2] = min(              minDiff[prefixA[l] % 2][prefixB[l] % 2], prefixA[l] - prefixB[l]);          ++l;        }        ans = max(ans, (prefixA.back() - prefixB.back()) -                           minDiff[1 - prefixA.back() % 2][prefixB.back() % 2]);      }    }     return ans;  }  private:  vector<pair<char, char>> getPermutations() {    vector<pair<char, char>> permutations;    for (const char a : "01234")      for (const char b : "01234")        if (a != b)          permutations.emplace_back(a, b);    return permutations;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗