题目:原题链接(困难)
标签:动态规划
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O ( M × N ) O(M×N) O(M×N) | O ( M × N ) O(M×N) O(M×N) | 336ms (89.39%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一:
MIN_INT = -10 ** 9
class Solution:
def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int:
m, n = len(nums1), len(nums2)
dp = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
dp[i][j] = max(nums1[i] * nums2[j] + (dp[i - 1][j - 1] if i > 0 and j > 0 else 0),
dp[i - 1][j] if i > 0 else 0,
dp[i][j - 1] if j > 0 else 0)
if dp[-1][-1] > 0:
return dp[-1][-1]
else:
return - abs(min(abs(num) for num in nums1)) * abs(min(abs(num) for num in nums2))