You are given two 0-indexed integer arrays nums1 and nums2 of equal length n and a positive integer k. You must choose a subsequence of indices from nums1 of length k.
For chosen indices i0, i1, …, ik - 1, your score is defined as:
The sum of the selected elements from nums1 multiplied with the minimum of the selected elements from nums2.
It can defined simply as: (nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] , nums2[i1], ... ,nums2[ik - 1]).
Return the maximum possible score.
A subsequence of indices of an array is a set that can be derived from the set {0, 1, ..., n-1} by deleting some or no elements.
Input:
nums1 =[1,3,3,2], nums2 =[2,1,3,4], k =3Output:
12Explanation:
The four possible subsequence scores are:- We choose the indices 0,1, and 2with score =(1+3+3)* min(2,1,3)=7.- We choose the indices 0,1, and 3with score =(1+3+2)* min(2,1,4)=6.- We choose the indices 0,2, and 3with score =(1+3+2)* min(2,3,4)=12.- We choose the indices 1,2, and 3with score =(3+3+2)* min(1,3,4)=8.Therefore, we return the max score, which is12.
1
2
3
4
5
6
7
8
9
10
11
12
#### Example 2
```d
```clike
Input:
nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1
Output:
30
Explanation:
Choosing index 2 is optimal: nums1[2] * nums2[2] = 3 * 10 = 30 is the maximum possible score.
To maximize the score, we want to select k pairs such that the sum of nums1 is as large as possible, and the minimum of nums2 among those pairs is also as large as possible. By sorting pairs by nums2 in descending order and maintaining the largest k values from nums1 using a min-heap, we can efficiently compute the best score.
classSolution {
public:longlong maxScore(vector<int>& nums1, vector<int>& nums2, int k) {
int n = nums1.size();
vector<pair<int,int>> arr(n);
for (int i =0; i < n; ++i) arr[i] = {nums2[i], nums1[i]};
sort(arr.rbegin(), arr.rend());
priority_queue<int, vector<int>, greater<int>> pq;
longlong sum =0, ans =0;
for (auto& [b, a] : arr) {
pq.push(a);
sum += a;
if (pq.size() > k) {
sum -= pq.top();
pq.pop();
}
if (pq.size() == k) ans = max(ans, sum * b);
}
return ans;
}
};
classSolution {
publiclongmaxScore(int[] nums1, int[] nums2, int k) {
int n = nums1.length;
int[][] arr =newint[n][2];
for (int i = 0; i < n; ++i) {
arr[i][0]= nums2[i];
arr[i][1]= nums1[i];
}
Arrays.sort(arr, (a, b) -> b[0]- a[0]);
PriorityQueue<Integer> pq =new PriorityQueue<>();
long sum = 0, ans = 0;
for (int[] p : arr) {
pq.offer(p[1]);
sum += p[1];
if (pq.size() > k) sum -= pq.poll();
if (pq.size() == k) ans = Math.max(ans, sum * p[0]);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funmaxScore(nums1: IntArray, nums2: IntArray, k: Int): Long {
val n = nums1.size
val arr = Array(n) { i -> nums2[i] to nums1[i] }
arr.sortByDescending { it.first }
val pq = java.util.PriorityQueue<Int>()
var sum = 0Lvar ans = 0Lfor ((b, a) in arr) {
pq.add(a)
sum += a
if (pq.size > k) sum -= pq.poll()
if (pq.size == k) ans = maxOf(ans, sum * b)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
defmax_score(nums1: list[int], nums2: list[int], k: int) -> int:
import heapq
arr = sorted(zip(nums2, nums1), reverse=True)
pq: list[int] = []
sum_ =0 ans =0for b, a in arr:
heapq.heappush(pq, a)
sum_ += a
if len(pq) > k:
sum_ -= heapq.heappop(pq)
if len(pq) == k:
ans = max(ans, sum_ * b)
return ans
impl Solution {
pubfnmax_score(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> i64 {
let n = nums1.len();
letmut arr: Vec<(i32, i32)>= (0..n).map(|i| (nums2[i], nums1[i])).collect();
arr.sort_by(|a, b| b.0.cmp(&a.0));
use std::collections::BinaryHeap;
letmut pq = BinaryHeap::new();
letmut sum =0i64;
letmut ans =0i64;
for (b, a) in arr {
pq.push(-a);
sum += a asi64;
if pq.len() > k asusize {
sum += pq.pop().unwrap() asi64;
}
if pq.len() == k asusize {
ans = ans.max(sum * b asi64);
}
}
ans
}
}