Problem statement

https://leetcode.com/problems/reveal-cards-in-increasing-order/

Solution

We can simulate our process in the reverse order using queue.

Complexity

It is O(n log n) for sorting part and O(n) for queue, so total O(n log n) time and O(n) space.

Code

class Solution:
    def deckRevealedIncreasing(self, deck):
        d = deque()
        for x in sorted(deck)[::-1]:
            d.appendleft(d.pop())   # or use d.rotate()
            d.appendleft(x)
        return list(d)