<p>You are given two <strong>sorted</strong> arrays of distinct integers <code>nums1</code> and <code>nums2</code>.</p>

<p>A <strong>valid<strong><em> </em></strong>path</strong> is defined as follows:</p>

<ul>
	<li>Choose array <code>nums1</code> or <code>nums2</code> to traverse (from index-0).</li>
	<li>Traverse the current array from left to right.</li>
	<li>If you are reading any value that is present in <code>nums1</code> and <code>nums2</code> you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).</li>
</ul>

<p>The <strong>score</strong> is defined as the sum of unique values in a valid path.</p>

<p>Return <em>the maximum score you can obtain of all possible <strong>valid paths</strong></em>. Since the answer may be too large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/16/sample_1_1893.png" style="width: 500px; height: 151px;" />
<pre>
<strong>Input:</strong> nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
<strong>Output:</strong> 30
<strong>Explanation:</strong> Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10],  (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10]    (starting from nums2)
The maximum is obtained with the path in green <strong>[2,4,6,8,10]</strong>.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1,3,5,7,9], nums2 = [3,5,100]
<strong>Output:</strong> 109
<strong>Explanation:</strong> Maximum sum is obtained with the path <strong>[1,3,5,100]</strong>.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
<strong>Output:</strong> 40
<strong>Explanation:</strong> There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>7</sup></code></li>
	<li><code>nums1</code> and <code>nums2</code> are strictly increasing.</li>
</ul>

class Solution {
public:
    int mod = 1e9+7;
    unordered_map<int, int> arr1_to_arr2;
    unordered_map<int, int> arr2_to_arr1;
    int maxSum(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int, int> val_to_idx;
        for(int i = 0; i < nums2.size(); i++) {
            val_to_idx[nums2[i]] = i;
        }
        
        arr1_to_arr2 = {};
        arr2_to_arr1 = {};
        for(int i = 0; i < nums1.size(); i++) {
            if (val_to_idx.find(nums1[i]) != val_to_idx.end()) {
                int arr2idx = val_to_idx[nums1[i]];
                arr1_to_arr2[i] = arr2idx;
                arr2_to_arr1[arr2idx] = i;
            }
        }
        int n = std::max(nums1.size(), nums2.size());
        vector<vector<long long>> memo(n, vector<long long>(2, -1));
        return std::max(func(nums1, nums2, 0, 0, memo), func(nums1, nums2, 0, 1, memo)) % mod;
    }

    long long func(vector<int>& a, vector<int>& b, int idx, int pos, vector<vector<long long>>& memo) {
        vector<int>& curr = pos == 0 ? a : b;
        if (idx >= curr.size()) {
            return 0;
        } else if (memo[idx][pos] != -1) {
           return memo[idx][pos];
        }
        
        unordered_map<int, int>& m = (pos == 0) ? arr1_to_arr2 : arr2_to_arr1;
        long long sum = curr[idx] + func(a, b, idx+1, pos, memo);
        // Try either going to the next idx in this array, or to the other array if we can.
        if (m.find(idx) != m.end()) {
            long long res = curr[idx] + func(a, b, m[idx]+1, (pos == 0 ? 1 : 0), memo);
            sum = std::max(sum, res);
        }
        memo[idx][pos] = sum;
        return sum;
    }
};
115086