Advantage Shuffle LeetCode solution in Python
1class Solution:2def advantageCount(self, nums1: List[int], nums2: List[int]) -> List[int]:3# sort by ascend order4nums1.sort()56# heapq in python is a minimal heap, so if we need to use a maximal heap, the element in the queue should be like: (-num, index)7q = []8for i, n in enumerate(nums2):9heapq.heappush(q, (-n, i))1011n = len(nums1)12l = 013r = n - 114ans = [0] * n1516while q:17# pop out the maximum number from nums218num, i = heapq.heappop(q)19num = -num2021# no number in nums1 > max number in nums2, so grap the minimum numer from nums122if nums1[r] <= num:23ans[i] = nums1[l]24l += 125else:26ans[i] = nums1[r]27r -= 12829return ans