Problem statement

https://binarysearch.com/problems/Ascending-Cards/

Solution

Equal to Leetcode 0950. Reveal Cards In Increasing Order.

Complexity

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

Code

class Solution:
    def solve(self, deck):
        n = len(deck)
        idx = deque(range(n))
        ans = [-1] * n
        for card in sorted(deck):
            ans[idx.popleft()] = card
            if idx: idx += [idx.popleft()]
                
        return ans