Reveal cards in increasing order
September 30, 2022
queueProblem URL: Reveal cards in increasing order
we will first sort the deck in descending order. Then we iterate over each card of the deck and append it to a queue. And before appending, if the queue is not empty, then we pop the card from the queue and append it back, so basically rotating the card. Then we append the new card in the queue. Finally we return all the elements of the queue as an array.
class Solution:
def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
deck.sort(reverse=True)
q = collections.deque()
for x in deck:
if q:
q.appendleft(q.pop())
q.appendleft(x)
return list(q)
Time Complexity: O(nlog(n))
Space Complexity: O(n)