Find Subsequence of Length K With the Largest Sum LeetCode solution in Java

1
class Solution {
2
public int[] maxSubsequence(int[] nums, int k) {
3
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[0] - a[0]);
4

5
for (int i = 0; i < nums.length; i++) pq.offer(new int[] {nums[i], i});
6

7
List<int[]> l = new ArrayList<>();
8

9
while (k-- != 0) l.add(pq.poll());
10

11
Collections.sort(l, (a, b) -> a[1] - b[1]);
12

13
int[] res = new int[l.size()];
14

15
int index = 0;
16

17
for (int[] i : l) res[index++] = i[0];
18

19
return res;
20
}
21
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0