Skip to content

950. Reveal Cards In Increasing Order 👍

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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)};
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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;
  }
}
1
2
3
4
5
6
7
8
9
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)
Back to top