1537. Get the Maximum Score Hard
1/**
2 * [1537] Get the Maximum Score
3 *
4 * You are given two sorted arrays of distinct integers nums1 and nums2.
5 * A valid path is defined as follows:
6 *
7 * Choose array nums1 or nums2 to traverse (from index-0).
8 * Traverse the current array from left to right.
9 * If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
10 *
11 * The score is defined as the sum of uniques values in a valid path.
12 * Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.
13 *
14 * Example 1:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2020/07/16/sample_1_1893.png" style="width: 500px; height: 151px;" />
16 * Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
17 * Output: 30
18 * Explanation: Valid paths:
19 * [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
20 * [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
21 * The maximum is obtained with the path in green [2,4,6,8,10].
22 *
23 * Example 2:
24 *
25 * Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
26 * Output: 109
27 * Explanation: Maximum sum is obtained with the path [1,3,5,100].
28 *
29 * Example 3:
30 *
31 * Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
32 * Output: 40
33 * Explanation: There are no common elements between nums1 and nums2.
34 * Maximum sum is obtained with the path [6,7,8,9,10].
35 *
36 *
37 * Constraints:
38 *
39 * 1 <= nums1.length, nums2.length <= 10^5
40 * 1 <= nums1[i], nums2[i] <= 10^7
41 * nums1 and nums2 are strictly increasing.
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/get-the-maximum-score/
47// discuss: https://leetcode.com/problems/get-the-maximum-score/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn max_sum(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
53 0
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_1537() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.