给出两个数组nums1和nums2,分别从他们之中取出等长的子数列做点乘,求可以获得的最大值,DP
1 #1458
2 #Runtime: 198 ms (Beats 50%)
3 #Memory: 15.7 MB (Beats 75%)
4
5 class Solution(object):
6 def maxDotProduct(self, nums1, nums2):
7 """
8 :type nums1: List[int]
9 :type nums2: List[int]
10 :rtype: int
11 """
12 n = len(nums1)
13 m = len(nums2)
14 dp = [[float('-inf')] * (m + 1) for _ in range(n + 1)]
15 for i in range(1, n + 1):
16 for j in range(1, m + 1):
17 dp[i][j] = max(nums1[i - 1] * nums2[j - 1], dp[i - 1][j - 1] + nums1[i - 1]* nums2[j - 1], dp[i - 1][j], dp[i][j - 1])
18 return dp[n][m]