Posted on

Description

Submission

class Solution {
public:
    int maxSum(vector<int>& nums1, vector<int>& nums2) {
        const long long p = 1e9 + 7;
        int n = nums1.size();
        int m = nums2.size();
        long long i = 0, j = 0;
        long long x = 0, y = 0;
        
        while(i < n || j < m) {
            if (i == n) {
                y += nums2[j++];
            } else if(j == m) {
                x += nums1[i++];
            } else if(nums1[i] < nums2[j]) {
                x += nums1[i++];
            } else if (nums1[i] > nums2[j]) {
                y += nums2[j++];
            } else if (nums1[i] == nums2[j]) {
                y = (max(x, y) + nums1[i]);
                x = y;
                i += 1;
                j += 1;
            }
        }
        
        return max(x, y) % p;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *