|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <openset.wang@gmail.com> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../minimum-swaps-to-arrange-a-binary-grid "Minimum Swaps to Arrange a Binary Grid") |
| 9 | + |
| 10 | +Next > |
| 11 | + |
| 12 | +## [1537. Get the Maximum Score (Hard)](https://leetcode.com/problems/get-the-maximum-score "最大得分") |
| 13 | + |
| 14 | +<p>You are given two <strong>sorted</strong> arrays of distinct integers <code>nums1</code> and <code>nums2.</code></p> |
| 15 | + |
| 16 | +<p>A <strong>valid<strong><em> </em></strong>path</strong> is defined as follows:</p> |
| 17 | + |
| 18 | +<ul> |
| 19 | + <li>Choose array nums1 or nums2 to traverse (from index-0).</li> |
| 20 | + <li>Traverse the current array from left to right.</li> |
| 21 | + <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> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<p><em>Score</em> is defined as the sum of uniques values in a valid path.</p> |
| 25 | + |
| 26 | +<p>Return the maximum <em>score</em> you can obtain of all possible <strong>valid paths</strong>.</p> |
| 27 | + |
| 28 | +<p>Since the answer may be too large, return it modulo 10^9 + 7.</p> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | +<p><strong>Example 1:</strong></p> |
| 32 | + |
| 33 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/16/sample_1_1893.png" style="width: 538px; height: 163px;" /></strong></p> |
| 34 | + |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] |
| 37 | +<strong>Output:</strong> 30 |
| 38 | +<strong>Explanation:</strong> Valid paths: |
| 39 | +[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) |
| 40 | +[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) |
| 41 | +The maximum is obtained with the path in green <strong>[2,4,6,8,10]</strong>. |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p><strong>Example 2:</strong></p> |
| 45 | + |
| 46 | +<pre> |
| 47 | +<strong>Input:</strong> nums1 = [1,3,5,7,9], nums2 = [3,5,100] |
| 48 | +<strong>Output:</strong> 109 |
| 49 | +<strong>Explanation:</strong> Maximum sum is obtained with the path <strong>[1,3,5,100]</strong>. |
| 50 | +</pre> |
| 51 | + |
| 52 | +<p><strong>Example 3:</strong></p> |
| 53 | + |
| 54 | +<pre> |
| 55 | +<strong>Input:</strong> nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] |
| 56 | +<strong>Output:</strong> 40 |
| 57 | +<strong>Explanation:</strong> There are no common elements between nums1 and nums2. |
| 58 | +Maximum sum is obtained with the path [6,7,8,9,10]. |
| 59 | +</pre> |
| 60 | + |
| 61 | +<p><strong>Example 4:</strong></p> |
| 62 | + |
| 63 | +<pre> |
| 64 | +<strong>Input:</strong> nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] |
| 65 | +<strong>Output:</strong> 61 |
| 66 | +</pre> |
| 67 | + |
| 68 | +<p> </p> |
| 69 | +<p><strong>Constraints:</strong></p> |
| 70 | + |
| 71 | +<ul> |
| 72 | + <li><code>1 <= nums1.length <= 10^5</code></li> |
| 73 | + <li><code>1 <= nums2.length <= 10^5</code></li> |
| 74 | + <li><code>1 <= nums1[i], nums2[i] <= 10^7</code></li> |
| 75 | + <li><code>nums1</code> and <code>nums2</code> are strictly increasing.</li> |
| 76 | +</ul> |
| 77 | + |
| 78 | +### Related Topics |
| 79 | + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] |
| 80 | + |
| 81 | +### Hints |
| 82 | +<details> |
| 83 | +<summary>Hint 1</summary> |
| 84 | +Partition the array by common integers, and choose the path with larger sum with a DP technique. |
| 85 | +</details> |
0 commit comments