[LeetCode] Maximum Multiplication Score

3290. Maximum Multiplication Score

You are given an integer array a of size 4 and another integer array b of size at least 4.

You need to choose 4 indices i0, i1, i2, and i3 from the array b such that i0 < i1 < i2 < i3. Your score will be equal to the value a[0] * b[i0] + a[1] * b[i1] + a[2] * b[i2] + a[3] * b[i3].

Return the maximum score you can achieve.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
long long maxScore(vector<int>& a, vector<int>& b) {
long long dp0 = LLONG_MIN, dp1 = LLONG_MIN, dp2 = LLONG_MIN, res = LLONG_MIN;
for(int i = b.size() - 1; i >= 0; i--) {
if(dp0 != LLONG_MIN) res = max(res, dp0 + 1ll * a[0] * b[i]);
if(dp1 != LLONG_MIN) dp0 = max(dp0, dp1 + 1ll * a[1] * b[i]);
if(dp2 != LLONG_MIN) dp1 = max(dp1, dp2 + 1ll * a[2] * b[i]);
dp2 = max(dp2, 1ll * a[3] * b[i]);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/09/15/PS/LeetCode/maximum-multiplication-score/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.