Leetcode 901 Solution

This article provides solution to leetcode question 901 (advantage-shuffle)

https://leetcode.com/problems/advantage-shuffle

Solution

class Solution:
    def advantageCount(self, A: List[int], B: List[int]) -> List[int]:
        A = sorted([(a, i) for i, a in enumerate(A)])
        B = sorted([(b, i) for i, b in enumerate(B)])

        i, j = 0, 0
        ans = []
        left_vals = []
        left_indexes = []
        while i < len(A) and j < len(B):
            if A[i][0] > B[j][0]:
                ans.append((B[j][1], A[i][0]))
                j += 1
            else:
                left_vals.append(A[i][0])
            i += 1

        while j < len(B):
            left_indexes.append(B[j][1])
            j += 1

        return [c for i, c in sorted(ans + list(zip(left_indexes, left_vals)))]