950. Reveal Cards In Increasing Order

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int[] deckRevealedIncreasing(int[] deck) {
Deque<Integer> deque = new LinkedList<>();
Arrays.sort(deck);
for (int i = deck.length - 1; i >= 0; i--) {
if (!deque.isEmpty()) {
deque.addFirst(deque.removeLast());
}
deque.addFirst(deck[i]);
}

int[] res = new int[deck.length];
for (int i = 0; i < res.length; i++) {
res[i] = deque.removeFirst();
}
return res;
}
}

Reference

950. Reveal Cards In Increasing Order