返回介绍

solution / 1500-1599 / 1537.Get the Maximum Score / README_EN

发布于 2024-06-17 01:03:18 字数 5824 浏览 0 评论 0 收藏 0

1537. Get the Maximum Score

中文文档

Description

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 unique 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.

Solutions

Solution 1

class Solution:
  def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
    mod = 10**9 + 7
    m, n = len(nums1), len(nums2)
    i = j = 0
    f = g = 0
    while i < m or j < n:
      if i == m:
        g += nums2[j]
        j += 1
      elif j == n:
        f += nums1[i]
        i += 1
      elif nums1[i] < nums2[j]:
        f += nums1[i]
        i += 1
      elif nums1[i] > nums2[j]:
        g += nums2[j]
        j += 1
      else:
        f = g = max(f, g) + nums1[i]
        i += 1
        j += 1
    return max(f, g) % mod
class Solution {
  public int maxSum(int[] nums1, int[] nums2) {
    final int mod = (int) 1e9 + 7;
    int m = nums1.length, n = nums2.length;
    int i = 0, j = 0;
    long f = 0, g = 0;
    while (i < m || j < n) {
      if (i == m) {
        g += nums2[j++];
      } else if (j == n) {
        f += nums1[i++];
      } else if (nums1[i] < nums2[j]) {
        f += nums1[i++];
      } else if (nums1[i] > nums2[j]) {
        g += nums2[j++];
      } else {
        f = g = Math.max(f, g) + nums1[i];
        i++;
        j++;
      }
    }
    return (int) (Math.max(f, g) % mod);
  }
}
class Solution {
public:
  int maxSum(vector<int>& nums1, vector<int>& nums2) {
    const int mod = 1e9 + 7;
    int m = nums1.size(), n = nums2.size();
    int i = 0, j = 0;
    long long f = 0, g = 0;
    while (i < m || j < n) {
      if (i == m) {
        g += nums2[j++];
      } else if (j == n) {
        f += nums1[i++];
      } else if (nums1[i] < nums2[j]) {
        f += nums1[i++];
      } else if (nums1[i] > nums2[j]) {
        g += nums2[j++];
      } else {
        f = g = max(f, g) + nums1[i];
        i++;
        j++;
      }
    }
    return max(f, g) % mod;
  }
};
func maxSum(nums1 []int, nums2 []int) int {
  const mod int = 1e9 + 7
  m, n := len(nums1), len(nums2)
  i, j := 0, 0
  f, g := 0, 0
  for i < m || j < n {
    if i == m {
      g += nums2[j]
      j++
    } else if j == n {
      f += nums1[i]
      i++
    } else if nums1[i] < nums2[j] {
      f += nums1[i]
      i++
    } else if nums1[i] > nums2[j] {
      g += nums2[j]
      j++
    } else {
      f = max(f, g) + nums1[i]
      g = f
      i++
      j++
    }
  }
  return max(f, g) % mod
}
function maxSum(nums1: number[], nums2: number[]): number {
  const mod = 1e9 + 7;
  const m = nums1.length;
  const n = nums2.length;
  let [f, g] = [0, 0];
  let [i, j] = [0, 0];
  while (i < m || j < n) {
    if (i === m) {
      g += nums2[j++];
    } else if (j === n) {
      f += nums1[i++];
    } else if (nums1[i] < nums2[j]) {
      f += nums1[i++];
    } else if (nums1[i] > nums2[j]) {
      g += nums2[j++];
    } else {
      f = g = Math.max(f, g) + nums1[i];
      i++;
      j++;
    }
  }
  return Math.max(f, g) % mod;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文