[C++] Dp problem, O(M+N) time, O(1) space
Explanation: Because both arrays are the increasing array including positive integer, that why dp is a good choice in this problem.\nWe have:\n- dp1[i] is the m
**Explanation**: Because both arrays are the increasing array including positive integer, that why dp is a good choice in this problem.\nWe have:\n- dp1[i] is the maximum of value if we start from any array and reach to index i of array 1.\n- dp2[i] has the same meaning: the maximum of value if we start from any array and reach to index i of array 2.\n\nTo calculate these dp arrays more effectively, we start from the beginning of each array and calculate with linear time.\nStart from i = 0 with array 1 and j = 0 with array 2.\n\n**Formula:**\n- dp1[i] = dp2[j] = max(dp1[i-1], dp2[j-1]) + nums1[i] with **nums1[i] = nums2[j]**\n- dp1[i] = dp1[i-1] + nums1[i] with **nums1[i] < nums2[j]**\n- dp2[j] = dp2[j-1] + nums2[j] with **nums2[j] < nums1[i]**\n\nBecause we always use the value from the previous index for each calculation in dp1 and dp2, therefore no need to store it in an array and can reduce the space complexity from O(N) to O(1).\n\nMy code:\n```\nclass Solution {\npublic:\n    int maxSum(vector<int>& nums1, vector<int>& nums2) {\n        int m = nums1.size(), n = nums2.size();\n        long dp1 = 0, dp2 = 0, res = 0;\n        for (int i = 0, j = 0; i < m || j < n;) {\n            if (i < m && j < n && nums1[i] == nums2[j])\n                dp1 = dp2 = max(dp1, dp2) + min(nums1[i++], nums2[j++]);\n            else if (i < m && ((j < n && nums1[i] < nums2[j]) || j == n))\n                dp1 += nums1[i++];\n            else\n                dp2 += nums2[j++];\n        }\n        int mod = 1e9 + 7;\n        return max(dp1, dp2) % mod;\n    }\n};\n```
[]