C++ Sliding Window (+Cheat Sheet)
See my latest update in repo LeetCode\n## Solution 1. Shrinkable Sliding Window\n\ncpp\n// OJ: https://leetcode.com/problems/maximum-erasure-value/\n// Author: 
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n## Solution 1. Shrinkable Sliding Window\n\n```cpp\n// OJ: https://leetcode.com/problems/maximum-erasure-value/\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Space: O(N)\nclass Solution {\npublic:\n    int maximumUniqueSubarray(vector<int>& A) {\n        int i = 0, ans = 0, N = A.size();\n        unordered_map<int, int> m; // number -> index of last occurrence.\n        vector<int> sum(N + 1);\n        partial_sum(begin(A), end(A), begin(sum) + 1);\n        for (int j = 0; j < N; ++j) {\n            if (m.count(A[j])) i = max(i, m[A[j]] + 1);\n            m[A[j]] = j;\n            ans = max(ans, sum[j + 1] - sum[i]);\n        }\n        return ans;\n    }\n};\n```\n\n## Solution 2. Shrinkable Sliding Window\n\n```cpp\n// OJ: https://leetcode.com/problems/maximum-erasure-value/\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Space: O(U) where U is the number of unique elements in `A`\nclass Solution {\npublic:\n    int maximumUniqueSubarray(vector<int>& A) {\n        int ans = 0, N = A.size(), sum = 0;\n        unordered_set<int> s;\n        for (int i = 0, j = 0; j < N; ++j) {\n            while (s.count(A[j])) {\n                s.erase(A[i]);\n                sum -= A[i++];\n            }\n            s.insert(A[j]);\n            sum += A[j];\n            ans = max(ans, sum);\n        }\n        return ans;\n    }\n};\n```\n\n## Solution 3. Shrinkable Sliding Window\n\nCheck out "[C++ Maximum Sliding Window Cheatsheet Template!](https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/1175088/C%2B%2B-Maximum-Sliding-Window-Cheatsheet-Template!)"\n\n```cpp\n// OJ: https://leetcode.com/problems/maximum-erasure-value/\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Space: O(U) where U is the number of unique elements in `A`\nclass Solution {\npublic:\n    int maximumUniqueSubarray(vector<int>& A) {\n        int i = 0, j = 0, N = A.size(), ans = 0, dup = 0, sum = 0;\n        unordered_map<int, int> cnt;\n        while (j < N) {\n            dup += ++cnt[A[j]] == 2;\n            sum += A[j++];\n            while (dup) {\n                dup -= --cnt[A[i]] == 1;\n                sum -= A[i++];\n            }\n            ans = max(ans, sum);\n        }\n        return ans;\n    }\n};\n```
[]