diff --git a/Hard/1458. Max Dot Product of Two Subsequences/solution.cpp b/Hard/1458. Max Dot Product of Two Subsequences/solution.cpp new file mode 100644 index 0000000..4d6594b --- /dev/null +++ b/Hard/1458. Max Dot Product of Two Subsequences/solution.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int maxDotProduct(vector& nums1, vector& nums2) { + int n = nums1.size(); + int m = nums2.size(); + vector> memo(n, vector(m, INT_MIN)); + + function 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); + } +}; \ No newline at end of file diff --git a/Hard/1458. Max Dot Product of Two Subsequences/solution.java b/Hard/1458. Max Dot Product of Two Subsequences/solution.java new file mode 100644 index 0000000..79a6a70 --- /dev/null +++ b/Hard/1458. Max Dot Product of Two Subsequences/solution.java @@ -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]; + } +} \ No newline at end of file diff --git a/Hard/1458. Max Dot Product of Two Subsequences/solution.md b/Hard/1458. Max Dot Product of Two Subsequences/solution.md new file mode 100644 index 0000000..c6b7071 --- /dev/null +++ b/Hard/1458. Max Dot Product of Two Subsequences/solution.md @@ -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. + + diff --git a/Hard/1458. Max Dot Product of Two Subsequences/solution.py b/Hard/1458. Max Dot Product of Two Subsequences/solution.py new file mode 100644 index 0000000..b1d7353 --- /dev/null +++ b/Hard/1458. Max Dot Product of Two Subsequences/solution.py @@ -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) \ No newline at end of file