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; }
|