[AlgoExpert] Max Subsequence Dot Product

Max Subsequence Dot Product

  • Time : O(nm)
  • Space : O(nm)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int maxSubsequenceDotProduct(vector<int> A, vector<int> B) {
int n = A.size(), m = B.size();
int res = A[0] * B[0];
vector<vector<int>> dp(n + 1, vector<int>(m + 1));
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
dp[i][j] = max({
dp[i-1][j-1] + A[i-1] * B[j-1],
dp[i-1][j],
dp[i][j-1],
dp[i-1][j-1]
});
res = max(res, dp[i][j]);
}
}
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/20/PS/AlgoExpert/max-subsequence-dot-product/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.