2542.Maximum Subsequence Score
===
###### tags: `Medium`,`Array`,`Greedy`,`Heap`,`Sorting`
[2542. Maximum Subsequence Score](https://leetcode.com/problems/maximum-subsequence-score/)
### 題目描述
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.
### 範例
**Example 1:**
```
Input: nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3
Output: 12
Explanation:
The four possible subsequence scores are:
- We choose the indices 0, 1, and 2 with score = (1+3+3) * min(2,1,3) = 7.
- We choose the indices 0, 1, and 3 with score = (1+3+2) * min(2,1,4) = 6.
- We choose the indices 0, 2, and 3 with score = (1+3+2) * min(2,3,4) = 12.
- We choose the indices 1, 2, and 3 with score = (3+3+2) * min(1,3,4) = 8.
Therefore, we return the max score, which is 12.
```
**Example 2:**
```
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.
```
**Constraints**:
* `n` == `nums1.length` == `nums2.length`
* 1 <= `n` <= 10^5^
* 0 <= `nums1[i]`, `nums2[j]` <= 10^5^
* 1 <= `k` <= `n`
### 解答
#### C++
``` cpp=
class Solution {
public:
long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {
ios_base::sync_with_stdio(0); cin.tie(0);
int n = nums1.size();
vector<pair<int, int>> numPairs;
for (int i = 0; i < n; i ++) {
numPairs.push_back({nums1[i], nums2[i]});
}
sort(numPairs.begin(), numPairs.end(), [](const auto p1, const auto p2) {
return p1.second > p2.second;
});
priority_queue<int, vector<int>, greater<int>> topkHeap;
long long topkSum = 0;
for (int i = 0; i < k; i ++) {
topkSum += numPairs[i].first;
topkHeap.push(numPairs[i].first);
}
long long ans = topkSum * numPairs[k - 1].second;
for (int i = k; i < n; i ++) {
topkSum += numPairs[i].first - topkHeap.top();
topkHeap.pop();
topkHeap.push(numPairs[i].first);
ans = max(ans, topkSum * numPairs[i].second);
}
return ans;
}
};
```
> [name=Jerry Wu][time=Wed, May24, 2023]
#### Python
```python=
class Solution:
def maxScore(self, nums1: List[int], nums2: List[int], k: int) -> int:
ans = total = 0
heap = []
data = sorted(zip(nums1, nums2), key=lambda x: -x[1])
for n1, n2 in data:
total += n1
heappush(heap, n1)
if len(heap) > k:
total -= heappop(heap)
if len(heap) == k:
ans = max(ans, total * n2)
return ans
```
> [name=Yen-Chi Chen][time=Wed, May 24, 2023]
```python=
class Solution:
def maxScore(self, nums1: List[int], nums2: List[int], k: int) -> int:
pairs = sorted(zip(nums1, nums2), key=lambda x: x[1], reverse=True)
heap = []
n1Sum, res = 0, 0
for n1, n2 in pairs:
n1Sum += n1
heapq.heappush(heap, n1)
if len(heap) > k:
n1Sum -= heappop(heap)
if len(heap) == k:
res = max(res, n1Sum * n2)
return res
```
> [name=Ron Chen][time=Wed, May 24, 2023]
### Reference
[回到題目列表](https://hackmd.io/@Marsgoat/leetcode_every_day)