C++ O(M+N) two pointers with explanation
See my latest update in repo LeetCode\n\n## Solution 1.\n\nIntuition:\n\nIf the two arrays don\'t have any number in common, the result is simply max(sum(A), su
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n## Solution 1.\n\n**Intuition:**\n\nIf the two arrays don\'t have any number in common, the result is simply `max(sum(A), sum(B))`.\n\nSo only the common numbers shared between these two arrays changes the algorithm a bit.\n\n**Algorithm:**\n\nConsidering the subproblem with `A[0..i)]` and `B[0..j)]`: \n\nLet `X` be the best score we can get if we pick `A[i]` in the end.\n\nLet `Y` be the best score we can get if we pick `B[j]` in the end.\n\nIf `A[i] < B[j]`, we can extend the `X` by adding `A[i]` to it.\n\nIf `A[i] > B[j]`, we can extend the `Y` by adding `B[j]` to it.\n\nIf `A[i] == B[j]`, we can extend in the following two ways:\n1. Use the previous value of `X`, and add `A[i]` to it.\n2. Use the previous value of `Y`, and add `B[j]` to it.\n\nWhy using the **previous value**? It\'s because we need to avoid adding the same number twice.\n\n```cpp\n// OJ: https://leetcode.com/contest/weekly-contest-200/problems/get-the-maximum-score/\n// Author: github.com/lzl124631x\n// Time: O(M + N)\n// Space: O(1)\nclass Solution {\npublic:\n    int maxSum(vector<int>& A, vector<int>& B) {\n        long long M = A.size(), N = B.size(), X = 0, Y = 0, prevX = 0, prevY = 0, mod = 1e9+7, i = 0, j = 0;\n        while (i < M && j < N) {\n            if (A[i] < B[j]) X += A[i++];\n            else if (A[i] > B[j]) Y += B[j++];\n            else X = Y = max(prevX + A[i++], prevY + B[j++]);\n            prevX = X;\n            prevY = Y;\n        }\n        while (i < M) X += A[i++];\n        while (j < N) Y += B[j++];\n        return max(X, Y) % mod;\n    }\n};\n```
[]