Java. 5 ms Beats 94.87% with PriorityQueue
Approach\nWe will use PriorityQueue to hold indexes of the largest numbers.\nThen we will use Set for fast checking if an index is in this set.\n\n# Complexity\
# Approach\nWe will use PriorityQueue to hold indexes of the largest numbers.\nThen we will use Set for fast checking if an index is in this set.\n\n# Complexity\n- Time complexity:\nO(N*logN)\n\n- Space complexity:\nO(N)\n\n# Code\n```\nclass Solution {\n    public int[] maxSubsequence(int[] nums, int k) {\n        PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> Integer.compare(nums[o1], nums[o2]));\n        for (int i = 0; i < nums.length; i++) {\n            if (pq.size() < k) {\n                pq.add(i); \n            } else if (nums[pq.peek()] < nums[i]) {\n                pq.poll(); // remove an index of the smallest number\n                pq.add(i);\n            }\n        }        \n        int[] res = new int[k];\n        Set<Integer> indexes = new HashSet<>(pq);\n        for (int i = 0, j = 0; i < nums.length; i++) {\n            if (indexes.contains(i)) {\n                res[j++] = nums[i];\n            }\n        }\n        return res;\n    }\n}\n```\n\nPlease Upvote \uD83D\uDC4D if you find this helpful!\n\u041F\u0440\u043E\u0433\u0440\u0430\u043C\u043C\u0430 \u043E\u0431\u0443\u0447\u0435\u043D\u0438\u044F Java (RU): www.javatutor.ru
['Heap (Priority Queue)', 'Java']