2542. 最大子序列的分数
小于 1 分钟
2542. 最大子序列的分数中等
解法思路:
- 根据
nums[2]
排序,同时根据更改的下标更新nums[1]
- 建立小根堆,
class Solution {
public long maxScore(int[] nums1, int[] nums2, int k) {
int n = nums1.length;
int[][] arr = new int[n][2];
for (int i = 0; i < n; i++) {
arr[i] = new int[]{nums1[i], nums2[i]};
}
Arrays.sort(arr, (a, b) -> (b[1] - a[1]));
PriorityQueue<Integer> pq = new PriorityQueue();
long res = 0;
long sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i][0];
pq.offer(arr[i][0]);
if (pq.size() == k) {
res = Math.max(res, sum * arr[i][1]);
sum -= pq.poll();
}
}
return res;
}
}
Powered by Waline v2.15.5