Problem statement

https://binarysearch.com/problems/Permute-to-Make-List-Larger/

Solution

Almost the same as BinarySearch 0214 Task Hare.

Complexity

It is O(n log n) for time and O(n) for space.

Code

class Solution:
    def solve(self, A, B):
        A = sorted(A)[::-1]
        B = sorted(B)[::-1]
        n = len(A)
        i = j = ans = 0

        while i < n and j < n:
            if A[j] > B[i]:
                ans += 1
                j += 1
            i += 1
        return ans