LeetCode Solutions

950. Reveal Cards In Increasing Order

Time:

Space:

			

class Solution {
 public:
  vector<int> deckRevealedIncreasing(vector<int>& deck) {
    sort(begin(deck), end(deck), greater<int>());

    deque<int> q{deck[0]};

    for (int i = 1; i < deck.size(); ++i) {
      q.push_front(q.back());
      q.pop_back();
      q.push_front(deck[i]);
    }

    return {begin(q), end(q)};
  }
};
			

class Solution {
  public int[] deckRevealedIncreasing(int[] deck) {
    final int n = deck.length;

    Arrays.sort(deck);

    Deque<Integer> q = new ArrayDeque<>();
    q.addFirst(deck[n - 1]);

    for (int i = n - 2; i >= 0; --i) {
      q.addFirst(q.getLast());
      q.pollLast();
      q.addFirst(deck[i]);
    }

    for (int i = 0; i < n; ++i)
      deck[i] = q.pollFirst();

    return deck;
  }
}
			

class Solution:
  def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
    q = deque()

    for card in reversed(sorted(deck)):
      q.rotate()
      q.appendleft(card)

    return list(q)