Python all solutions using sort and heap | sort | heap | one-liner
Using sort\n\nTime: O(nlogn)\npython\nclass Solution:\n    def maxSubsequence(self, nums: List[int], k: int) -> List[int]:\n        arr = [(nums[i], i) for i in
# Using sort\n\nTime: `O(nlogn)`\n```python\nclass Solution:\n    def maxSubsequence(self, nums: List[int], k: int) -> List[int]:\n        arr = [(nums[i], i) for i in range(len(nums))]\n        arr.sort(reverse=True)\n\n        arr = arr[:k]\n        arr.sort(key=lambda k: k[1])\n\n        return [val[0] for val in arr]\n\n```\n\n# Using sort (one-liner)\n\nTime: `O(nlogn)`\n```python\nclass Solution:\n    def maxSubsequence(self, nums: List[int], k: int) -> List[int]:\n        return [\n            nums[x]\n            for x in sorted(\n                sorted(range(len(nums)), key=lambda k: nums[k], reverse=True)[:k]\n            )\n        ]\n\n```\n\n# Using heap\n\nTime: `O(max(n, klogn, klogk))`\n```python\nclass Solution:\n    def maxSubsequence(self, nums: List[int], k: int) -> List[int]:\n        arr = [(-nums[i], i) for i in range(len(nums))]\n        heapq.heapify(arr)\n\n        ans = []\n        while k > 0:\n            ans.append(heapq.heappop(arr))\n            k -= 1\n\n        ans.sort(key=lambda k: k[1])\n        return [-val[0] for val in ans]\n\n```
['Sorting', 'Heap (Priority Queue)', 'Python', 'Python3']