LeetCode每日一题(1537. Get the Maximum Score)

该博客主要介绍了如何在两个有序数组中找到一条有效的路径,使得路径上不同元素的和最大。有效路径定义为从一个数组开始,遇到相同元素时可以切换到另一个数组,并且每个元素只计一次。给出了一种使用递归和前缀和的方法来解决此问题,并给出了若干示例来解释其工作原理和输出结果。
摘要由CSDN通过智能技术生成

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
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值