This repository has been archived by the owner on Oct 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from kushal34712/patch-3
Create kushalaqud792
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/// help of squd | ||
|
||
|
||
class Solution { | ||
public: | ||
int maxDotProduct(vector<int>& nums1, vector<int>& nums2) { | ||
int m = nums1.size(); | ||
int n = nums2.size(); | ||
|
||
// Initialize a 2D DP array to store the maximum dot product | ||
vector<vector<int>> dp(m, vector<int>(n)); | ||
|
||
// Fill in the DP array | ||
dp[0][0] = nums1[0] * nums2[0]; | ||
int max_product = dp[0][0]; | ||
|
||
for (int i = 1; i < m; i++) { | ||
dp[i][0] = max(dp[i - 1][0], nums1[i] * nums2[0]); | ||
max_product = max(max_product, dp[i][0]); | ||
} | ||
|
||
for (int j = 1; j < n; j++) { | ||
dp[0][j] = max(dp[0][j - 1], nums1[0] * nums2[j]); | ||
max_product = max(max_product, dp[0][j]); | ||
} | ||
|
||
for (int i = 1; i < m; i++) { | ||
for (int j = 1; j < n; j++) { | ||
dp[i][j] = max(nums1[i] * nums2[j], dp[i - 1][j - 1] + nums1[i] * nums2[j]); | ||
dp[i][j] = max(dp[i][j], dp[i - 1][j]); | ||
dp[i][j] = max(dp[i][j], dp[i][j - 1]); | ||
max_product = max(max_product, dp[i][j]); | ||
} | ||
} | ||
|
||
return max_product; | ||
} | ||
}; | ||
|
||
|
||
//Leetcode POTD | ||
//CPP | ||
//8th Oct |