先用array里的数字的负值和其index组成subarray,放到一个heap里,从heap里往外弹出k个subarray。依据index对k个subarray排序。然后按顺序取负值的负值。
class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
container = []
for i,nums in enumerate(nums):
heapq.heappush(container, (-nums, i))
tempres = []
for _ in range(k):
tempres.append(heapq.heappop(container))
tempres.sort(key = lambda x:x[1])
res = []
for num, pos in tempres:
res.append(-num)
return res