Advantage Shuffle

Problem Id: 870 Difficulty: Medium


Intuition

Solution


class Solution:
    def advantageCount(self, A: List[int], B: List[int]) -> List[int]:
        A.sort()
        B = [(B[i], i) for i in range(len(B))]
        B.sort(reverse=True)
        permutation = [None] * len(B)
        lo = 0
        hi = len(B) - 1
        for num, index in B:
            if A[hi] > num:
                permutation[index] = A[hi]
                hi -= 1
            else:
                permutation[index] = A[lo]
                lo += 1
        return permutation