Skip to content

Commit

Permalink
solution(cpp,java,python3): 1458. Max Dot Product of Two Subsequences
Browse files Browse the repository at this point in the history
  • Loading branch information
godkingjay authored Oct 9, 2023
2 parents 6ded25b + c7eb55e commit 0c9f3b2
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Hard/1458. Max Dot Product of Two Subsequences/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
int maxDotProduct(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size();
int m = nums2.size();
vector<vector<int>> memo(n, vector<int>(m, INT_MIN));

function<int(int, int)> dp = [&](int i, int j) {
if (i == n || j == m) {
return INT_MIN;
}

if (memo[i][j] != INT_MIN) {
return memo[i][j];
}

memo[i][j] = max(
nums1[i] * nums2[j] + max(dp(i + 1, j + 1), 0),
max(dp(i + 1, j), dp(i, j + 1))
);

return memo[i][j];
};

return dp(0, 0);
}
};
35 changes: 35 additions & 0 deletions Hard/1458. Max Dot Product of Two Subsequences/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public int maxDotProduct(int[] nums1, int[] nums2) {
int n = nums1.length;
int m = nums2.length;
int[][] memo = new int[n][m];

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
memo[i][j] = Integer.MIN_VALUE;
}
}

return dp(nums1, nums2, 0, 0, memo);
}

private int dp(int[] nums1, int[] nums2, int i, int j, int[][] memo) {
int n = nums1.length;
int m = nums2.length;

if (i == n || j == m) {
return Integer.MIN_VALUE;
}

if (memo[i][j] != Integer.MIN_VALUE) {
return memo[i][j];
}

memo[i][j] = Math.max(
nums1[i] * nums2[j] + Math.max(dp(nums1, nums2, i + 1, j + 1, memo), 0),
Math.max(dp(nums1, nums2, i + 1, j, memo), dp(nums1, nums2, i, j + 1, memo))
);

return memo[i][j];
}
}
36 changes: 36 additions & 0 deletions Hard/1458. Max Dot Product of Two Subsequences/solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Problem Title

Max Dot Product of Two Subsequences

## Problem Description

1. We need to find max dot product possible given two arrays nums1 and nums2.

2. We need to ensure that while finding the dot product, the relative order of the numbers in respective arrays is same.

3. We can achieve this using dynamic programming. This is because, at any point when we find the dot product of two numbers we need to add the maximum dot product till previous element to our current answer. This we do till all elements of both the arrays are accounted for.

## Method to Solve

1. The function maxDotProduct takes two lists, nums1 and nums2, as input and returns an integer, which is the maximum dot product.

2. It initializes n and m as the lengths of nums1 and nums2, respectively.

3. It creates a 2D memoization table memo of size n x m and initializes all its values to negative infinity (indicating that they have not been computed yet).

4. The core of the algorithm is the dp function, which is a recursive function that computes the maximum dot product starting from a specific position (i, j) in the two arrays.

5. The base case of the recursion is when either i reaches n or j reaches m, in which case the dot product cannot be calculated, so it returns negative infinity.

6. If the value at (i, j) in the memoization table is not negative infinity (indicating it has been computed before), the function directly returns that value.

7. Otherwise, it calculates the maximum dot product at position (i, j) by considering three choices:
* The dot product of nums1[i] and nums2[j] plus the maximum dot product starting from the next positions (i+1, j+1) with a minimum value of 0 (ensuring that negative contributions are not considered).
* The maximum dot product starting from (i+1, j) without including the current elements from nums1 and nums2.
* The maximum dot product starting from (i, j+1) without including the current elements from nums1 and nums2.

8. The maximum of these three choices is stored in memo[i][j], and the function returns this value.

9. Finally, the maxDotProduct function is called with an initial position of (0, 0) to start the computation, and it returns the maximum dot product.


21 changes: 21 additions & 0 deletions Hard/1458. Max Dot Product of Two Subsequences/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int:
n, m = len(nums1), len(nums2)
memo = [[float('-inf')] * m for _ in range(n)]

def dp(i, j):
if i == n or j == m:
return float('-inf')

if memo[i][j] != float('-inf'):
return memo[i][j]

memo[i][j] = max(
nums1[i] * nums2[j] + max(dp(i + 1, j + 1), 0),
dp(i + 1, j),
dp(i, j + 1),
)

return memo[i][j]

return dp(0, 0)

0 comments on commit 0c9f3b2

Please sign in to comment.