-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxScoreSubsequence.java
More file actions
53 lines (39 loc) · 1.34 KB
/
Copy pathMaxScoreSubsequence.java
File metadata and controls
53 lines (39 loc) · 1.34 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package LeetcodePractice;
import java.util.*;
public class MaxScoreSubsequence {
public long maxScore(int[] nums1, int[] nums2, int k) {
int n = nums1.length;
int[][] pairs = new int[n][2];
for (int i = 0; i < n; i++) {
pairs[i][0] = nums2[i];
pairs[i][1] = nums1[i];
}
Arrays.sort(pairs, (a, b) -> b[0] - a[0]);
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
long sum = 0, maxScore = 0;
for (int[] pair : pairs) {
int curNum2 = pair[0];
int curNum1 = pair[1];
minHeap.offer(curNum1);
sum += curNum1;
if (minHeap.size() > k) {
sum -= minHeap.poll();
}
if (minHeap.size() == k) {
maxScore = Math.max(maxScore, sum * curNum2);
}
}
return maxScore;
}
public static void main(String[] args) {
MaxScoreSubsequence solver = new MaxScoreSubsequence();
int[] nums1_1 = {1, 3, 3, 2};
int[] nums2_1 = {2, 1, 3, 4};
int k1 = 3;
System.out.println(solver.maxScore(nums1_1, nums2_1, k1));
int[] nums1_2 = {4, 2, 3, 1, 1};
int[] nums2_2 = {7, 5, 10, 9, 6};
int k2 = 1;
System.out.println(solver.maxScore(nums1_2, nums2_2, k2));
}
}