返回介绍

solution / 1400-1499 / 1458.Max Dot Product of Two Subsequences / README_EN

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

1458. Max Dot Product of Two Subsequences

中文文档

Description

Given two arrays nums1 and nums2.

Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length.

A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).

 

Example 1:

Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6]
Output: 18
Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2.
Their dot product is (2*3 + (-2)*(-6)) = 18.

Example 2:

Input: nums1 = [3,-2], nums2 = [2,-6,7]
Output: 21
Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2.
Their dot product is (3*7) = 21.

Example 3:

Input: nums1 = [-1,-1], nums2 = [1,1]
Output: -1
Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2.
Their dot product is -1.

 

Constraints:

  • 1 <= nums1.length, nums2.length <= 500
  • -1000 <= nums1[i], nums2[i] <= 1000

Solutions

Solution 1

class Solution:
  def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int:
    m, n = len(nums1), len(nums2)
    dp = [[-inf] * (n + 1) for _ in range(m + 1)]
    for i in range(1, m + 1):
      for j in range(1, n + 1):
        v = nums1[i - 1] * nums2[j - 1]
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1], max(dp[i - 1][j - 1], 0) + v)
    return dp[-1][-1]
class Solution {
  public int maxDotProduct(int[] nums1, int[] nums2) {
    int m = nums1.length, n = nums2.length;
    int[][] dp = new int[m + 1][n + 1];
    for (int[] e : dp) {
      Arrays.fill(e, Integer.MIN_VALUE);
    }
    for (int i = 1; i <= m; ++i) {
      for (int j = 1; j <= n; ++j) {
        dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
        dp[i][j] = Math.max(
          dp[i][j], Math.max(0, dp[i - 1][j - 1]) + nums1[i - 1] * nums2[j - 1]);
      }
    }
    return dp[m][n];
  }
}
class Solution {
public:
  int maxDotProduct(vector<int>& nums1, vector<int>& nums2) {
    int m = nums1.size(), n = nums2.size();
    vector<vector<int>> dp(m + 1, vector<int>(n + 1, INT_MIN));
    for (int i = 1; i <= m; ++i) {
      for (int j = 1; j <= n; ++j) {
        int v = nums1[i - 1] * nums2[j - 1];
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        dp[i][j] = max(dp[i][j], max(0, dp[i - 1][j - 1]) + v);
      }
    }
    return dp[m][n];
  }
};
func maxDotProduct(nums1 []int, nums2 []int) int {
  m, n := len(nums1), len(nums2)
  dp := make([][]int, m+1)
  for i := range dp {
    dp[i] = make([]int, n+1)
    for j := range dp[i] {
      dp[i][j] = math.MinInt32
    }
  }
  for i := 1; i <= m; i++ {
    for j := 1; j <= n; j++ {
      v := nums1[i-1] * nums2[j-1]
      dp[i][j] = max(dp[i-1][j], dp[i][j-1])
      dp[i][j] = max(dp[i][j], max(0, dp[i-1][j-1])+v)
    }
  }
  return dp[m][n]
}
impl Solution {
  #[allow(dead_code)]
  pub fn max_dot_product(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
    let n = nums1.len();
    let m = nums2.len();
    let mut dp = vec![vec![i32::MIN; m + 1]; n + 1];

    // Begin the actual dp process
    for i in 1..=n {
      for j in 1..=m {
        dp[i][j] = std::cmp::max(
          std::cmp::max(dp[i - 1][j], dp[i][j - 1]),
          std::cmp::max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1]
        );
      }
    }

    dp[n][m]
  }
}

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

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

发布评论

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