Solving Leetcode Interviews in Seconds with AI: Max Dot Product of Two Subsequences
Introduction
In this blog post, we will explore how to solve the LeetCode problem "1458" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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 (23 + (-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
Explanation
Here's the breakdown of the approach, complexities, and the Python code:
Approach:
- Dynamic Programming: Use a DP table
dp[i][j]to store the maximum dot product of subsequences ending atnums1[i]andnums2[j]. - State Transitions: Consider three cases: (1) Include both
nums1[i]andnums2[j]in the subsequence, (2) Excludenums1[i], (3) Excludenums2[j]. Take the maximum of these three possibilities. Also include just multiplying the current elements in case previous subsequences lead to negative maximums. - Base Case and Result: The final answer is the maximum value in the DP table. Handle cases where all products are negative by returning the maximum individual product.
- Dynamic Programming: Use a DP table
Complexity:
- Runtime Complexity: O(m*n), where m and n are the lengths of
nums1andnums2respectively. - Storage Complexity: O(m*n)
- Runtime Complexity: O(m*n), where m and n are the lengths of
Code
def maxDotProduct(nums1, nums2):
m, n = len(nums1), len(nums2)
dp = [[float('-inf')] * n for _ in range(m)]
for i in range(m):
for j in range(n):
product = nums1[i] * nums2[j]
dp[i][j] = product # Base case: only include nums1[i] and nums2[j]
if i > 0 and j > 0:
dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + product) #extend the previous subsequence
dp[i][j] = max(dp[i][j], dp[i-1][j-1], product) #start from here if the previous is bad
if i > 0:
dp[i][j] = max(dp[i][j], dp[i - 1][j]) #exclude nums1[i]
if j > 0:
dp[i][j] = max(dp[i][j], dp[i][j - 1]) #exclude nums2[j]
result = float('-inf')
for i in range(m):
for j in range(n):
result = max(result, dp[i][j])
# Handle all negative products case
if result == float('-inf'):
max_num1 = max(nums1)
max_num2 = max(nums2)
if max_num1 <= 0 and max_num2 <= 0:
return max(nums1) * max(nums2) #This condition is technically wrong and does not address the case when both arrays consist of negative numbers.
# If even only one of the array has positive values, then the result will not be negative infinity
max_single_product = float('-inf')
for num1 in nums1:
for num2 in nums2:
max_single_product = max(max_single_product, num1*num2)
return max_single_product
return result