You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
Choose array nums1 or nums2 to traverse (from index-0).
Traverse the current array from left to right.
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).
The score is defined as the sum of uniques values in a valid path.
Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 109 + 7.
Example 1:
Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: 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 [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Constraints:
- 1 <= nums1.length, nums2.length <= 105
- 1 <= nums1[i], nums2[i] <= 107
- nums1 and nums2 are strictly increasing.
思路不难, 就是每次找一个可以横跨两个数组的点(相同的元素), 分别计算两个数组从这一点到前一个横跨点之间的和, 然后取大的, 注意头尾都可能是开区间, 也就是只有一个横跨点的情况。还有需要注意的就是不是路径上的点都计分, 重复的元素只计一次分
use std::collections::HashMap;
impl Solution {
fn cross_position(
nums1: &Vec<i32>,
indices2: &HashMap<i32, usize>,
mut i: usize,
) -> Option<(usize, usize)> {
while i < nums1.len() {
let n1 = nums1[i];
if let Some(&j) = indices2.get(&n1) {
return Some((i, j));
}
i += 1;
}
None
}
fn rc(
nums1: &Vec<i32>,
nums2: &Vec<i32>,
presum1: &Vec<i128>,
indices1: &HashMap<i32, usize>,
presum2: &Vec<i128>,
indices2: &HashMap<i32, usize>,
s1: usize,
s2: usize,
) -> i128 {
if let Some((e1, e2)) = Solution::cross_position(nums1, indices2, s1) {
let score1 = presum1[e1 + 1] - presum1[s1];
let score2 = presum2[e2 + 1] - presum2[s2];
if score2 > score1 {
return score2 as i128
+ Solution::rc(
nums2,
nums1,
presum2,
indices2,
presum1,
indices1,
e2 + 1,
e1 + 1,
);
}
return score1 as i128
+ Solution::rc(
nums1,
nums2,
presum1,
indices1,
presum2,
indices2,
e1 + 1,
e2 + 1,
);
}
let score1 = *presum1.last().unwrap() - presum1[s1];
let score2 = *presum2.last().unwrap() - presum2[s2];
score2.max(score1) as i128
}
pub fn max_sum(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
let mut presum1 = vec![0];
let mut indices1 = HashMap::new();
let mut sum = 0i128;
for (i, &v) in nums1.iter().enumerate() {
sum += v as i128;
presum1.push(sum);
indices1.insert(v, i);
}
let mut presum2 = vec![0];
let mut indices2 = HashMap::new();
let mut sum = 0i128;
for (i, &v) in nums2.iter().enumerate() {
sum += v as i128;
presum2.push(sum);
indices2.insert(v, i);
}
(Solution::rc(
&nums1, &nums2, &presum1, &indices1, &presum2, &indices2, 0, 0,
) % (10i128.pow(9) + 7)) as i32
}
}