gary@interview:~/interview/coding/870-advantage-sh….md$
$ cat ./coding/870-advantage-shuffle.md
[Coding]

870. Advantage Shuffle

────────────────────────────────────────────────────────────

870. Advantage Shuffle

class Solution:
    def advantageCount(self, nums1: List[int], nums2: List[int]) -> List[int]:
        
        n = len(nums1)
        nums1.sort()
        heap = []

        for i in range(len(nums2)):
            num = nums2[i]
            # Pytho 的 heap 是最小的在最上面,但是我們要的是最大的要在最上面。
            heapq.heappush(heap, (-num, i))

        left = 0
        right = n - 1
        res = [0] * n

        while heap:
            val, idx = heapq.heappop(heap)
            val = val * -1
            if val < nums1[right]:
                res[idx] = nums1[right]
                right -= 1
            else:
                res[idx] = nums1[left]
                left += 1
        return res
--tags#Heap#Two Pointers
$ ls ./coding/ | grep -v 870-advantage-shuffle
265. Paint House II256. Paint House143. Reorder List1762. Buildings With an Ocean View
← cd ../codingcd ~