Find Subsequence of Length K With the Largest Sum LeetCode solution in Java
1class Solution {2public int[] maxSubsequence(int[] nums, int k) {3PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[0] - a[0]);45for (int i = 0; i < nums.length; i++) pq.offer(new int[] {nums[i], i});67List<int[]> l = new ArrayList<>();89while (k-- != 0) l.add(pq.poll());1011Collections.sort(l, (a, b) -> a[1] - b[1]);1213int[] res = new int[l.size()];1415int index = 0;1617for (int[] i : l) res[index++] = i[0];1819return res;20}21}