3836. Maximum Score Using Exactly K Pairs
Description
You are given two integer arrays nums1 and nums2 of lengths n and m respectively, and an integer k.
You must choose exactly k pairs of indices (i1, j1), (i2, j2), ..., (ik, jk) such that:
0 <= i1 < i2 < ... < ik < n0 <= j1 < j2 < ... < jk < m
For each chosen pair (i, j), you gain a score of nums1[i] * nums2[j].
The total score is the sum of the products of all selected pairs.
Return an integer representing the maximum achievable total score.
Example 1:
Input: nums1 = [1,3,2], nums2 = [4,5,1], k = 2
Output: 22
Explanation:
One optimal choice of index pairs is:
(i1, j1) = (1, 0)which scores3 * 4 = 12(i2, j2) = (2, 1)which scores2 * 5 = 10
This gives a total score of 12 + 10 = 22.
Example 2:
Input: nums1 = [-2,0,5], nums2 = [-3,4,-1,2], k = 2
Output: 26
Explanation:
One optimal choice of index pairs is:
(i1, j1) = (0, 0)which scores-2 * -3 = 6(i2, j2) = (2, 1)which scores5 * 4 = 20
The total score is 6 + 20 = 26.
Example 3:
Input: nums1 = [-3,-2], nums2 = [1,2], k = 2
Output: -7
Explanation:
The optimal choice of index pairs is:
(i1, j1) = (0, 0)which scores-3 * 1 = -3(i2, j2) = (1, 1)which scores-2 * 2 = -4
The total score is -3 + (-4) = -7.
Constraints:
1 <= n == nums1.length <= 1001 <= m == nums2.length <= 100-106 <= nums1[i], nums2[i] <= 1061 <= k <= min(n, m)
Solutions
Solution 1: Dynamic Programming
Thinking
Pick \(k\) index-increasing pairs from two arrays and maximize the sum of products. \(n,m \le 100\) and \(k \le \min(n,m)\) fit a 3-D DP.
A pair consumes one element from each side and cannot go backwards. The choices are skip \(nums1\), skip \(nums2\), or pair both.
Let \(f[i][j][k]\) be the best score using the two prefixes and exactly \(k\) pairs, with those three transitions.
The empty prefixes with zero pairs score \(0\); other states start at \(-\infty\). The answer is \(f[n][m][K]\).
We denote the lengths of arrays \(\textit{nums1}\) and \(\textit{nums2}\) as \(n\) and \(m\) respectively, and denote \(k\) in the problem as \(K\).
We define a three-dimensional array \(f\), where \(f[i][j][k]\) represents the maximum score of selecting exactly \(k\) index pairs from the first \(i\) elements of \(\textit{nums1}\) and the first \(j\) elements of \(\textit{nums2}\). Initially, \(f[0][0][0] = 0\), and all other values of \(f[i][j][k]\) are negative infinity.
We can calculate \(f[i][j][k]\) through the following state transition equation:
The first case represents not selecting the \(i\)-th element of \(\textit{nums1}\), the second case represents not selecting the \(j\)-th element of \(\textit{nums2}\), and the third case represents selecting the \(i\)-th element of \(\textit{nums1}\) and the \(j\)-th element of \(\textit{nums2}\) as a pair of indices.
Finally, we need to return \(f[n][m][K]\).
The time complexity is \(O(m \times n \times K)\) and the space complexity is \(O(m \times n \times K)\), where \(n\) and \(m\) are the lengths of arrays \(\textit{nums1}\) and \(\textit{nums2}\) respectively, and \(K\) is \(k\) in the problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |