Dashboard Temp Share Shortlinks Frames API

HTMLify

max-dot-product-of-two-subsequences.py
Views: 10 | Author: prakhardoneria
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution:
    def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int:
        n, m = len(nums1), len(nums2)
        dp = [[-float('inf')] * m for _ in range(n)]

        for i in range(n):
            for j in range(m):
                current_prod = nums1[i] * nums2[j]
                
                dp[i][j] = current_prod
                if i > 0 and j > 0:
                    dp[i][j] = max(dp[i][j], current_prod + dp[i-1][j-1])
                
                if i > 0:
                    dp[i][j] = max(dp[i][j], dp[i-1][j])
                if j > 0:
                    dp[i][j] = max(dp[i][j], dp[i][j-1])
                    
        return dp[n-1][m-1]