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].
Input: a =[3,2,5,6], b =[2,-6,4,-5,-3,2,-7]Output: 26Explanation:
We can choose the indices 0,1,2, and 5. The score will be `3 * 2 + 2 * (-6)
+ 5 * 4 + 6 * 2 = 26`.
Input: a =[-1,4,5,-2], b =[-5,-1,-3,-2,-4]Output: -1Explanation:
We can choose the indices 0,1,3, and 4. The score will be `(-1) * (-5) + 4 *
(-1) + 5 * (-2) + (-2) * (-4) = -1`.
We need to pick 4 increasing indices from b and assign them to a[0], a[1], a[2], a[3] in order to maximize the sum a[0]*b[i0] + a[1]*b[i1] + a[2]*b[i2] + a[3]*b[i3]. Since a has only 4 elements, we can use dynamic programming where the state is the number of elements picked so far and the current index in b.
classSolution {
publicintmaximumScore(int[] a, int[] b) {
int n = b.length;
int[][] dp =newint[5][n];
for (int i = 0; i < 5; ++i) Arrays.fill(dp[i], Integer.MIN_VALUE);
for (int j = 0; j < n; ++j) dp[0][j]= 0;
for (int i = 1; i <= 4; ++i) {
for (int j = i-1; j < n; ++j) {
for (int k = i-2; k < j; ++k) {
if (dp[i-1][k]!= Integer.MIN_VALUE)
dp[i][j]= Math.max(dp[i][j], dp[i-1][k]+ a[i-1]*b[j]);
}
}
}
int ans = Integer.MIN_VALUE;
for (int j = 3; j < n; ++j) ans = Math.max(ans, dp[4][j]);
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funmaximumScore(a: IntArray, b: IntArray): Int {
val n = b.size
val dp = Array(5) { IntArray(n) { Int.MIN_VALUE } }
for (j in0 until n) dp[0][j] = 0for (i in1..4) {
for (j in i-1 until n) {
for (k in i-2 until j) {
if (dp[i-1][k] !=Int.MIN_VALUE)
dp[i][j] = maxOf(dp[i][j], dp[i-1][k] + a[i-1]*b[j])
}
}
}
var ans = Int.MIN_VALUE
for (j in3 until n) ans = maxOf(ans, dp[4][j])
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defmaximumScore(self, a: list[int], b: list[int]) -> int:
n = len(b)
dp = [[float('-inf')] * n for _ in range(5)]
for j in range(n):
dp[0][j] =0for i in range(1, 5):
for j in range(i-1, n):
for k in range(i-2, j):
if dp[i-1][k] != float('-inf'):
dp[i][j] = max(dp[i][j], dp[i-1][k] + a[i-1]*b[j])
ans = float('-inf')
for j in range(3, n):
ans = max(ans, dp[4][j])
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
impl Solution {
pubfnmaximum_score(a: Vec<i32>, b: Vec<i32>) -> i32 {
let n = b.len();
letmut dp =vec![vec![i32::MIN; n]; 5];
for j in0..n { dp[0][j] =0; }
for i in1..=4 {
for j in (i-1)..n {
for k in (i-2)..j {
if dp[i-1][k] !=i32::MIN {
dp[i][j] = dp[i][j].max(dp[i-1][k] + a[i-1]*b[j]);
}
}
}
}
letmut ans =i32::MIN;
for j in3..n { ans = ans.max(dp[4][j]); }
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
maximumScore(a: number[], b: number[]):number {
constn=b.length;
constdp= Array.from({length: 5}, () => Array(n).fill(-Infinity));
for (letj=0; j<n; ++j) dp[0][j] =0;
for (leti=1; i<=4; ++i) {
for (letj=i-1; j<n; ++j) {
for (letk=i-2; k<j; ++k) {
if (dp[i-1][k] !==-Infinity)
dp[i][j] = Math.max(dp[i][j], dp[i-1][k] +a[i-1]*b[j]);
}
}
}
letans=-Infinity;
for (letj=3; j<n; ++j) ans= Math.max(ans, dp[4][j]);
returnans;
}
}