# code tpl
950. Reveal Cards In Increasing Order
{%hackmd theme-dark %}
original:
[17,13,11,2,3,5,7]
ans:
[2,13,3,11,5,17,7]
two op: pickup first and pushback sec
2
3, 11, 5, 17, 7, 13
2, 3
5, 17, 7, 13, 11
2,3,5
7, 13, 11, 17
....
[2, 3, 5, 7, 11, 13, 17]
[2, 13, 3, 11, 5, 17, 7]
[2, 13,3,11,5,17,7]
11 13 17
0 1 2 3 4 5 6
simulate idxes put in sorted value
dequeue
0 ->2 [2 3 4 5 6 1]
2 [4 5 6 1 3]
4 [6 1 3 5]
6 [3 5 1]
3 [1 5]
1 [5]
5
```python=
class Solution(object):
def deckRevealedIncreasing(self, deck):
"""
:type deck: List[int]
:rtype: List[int]
"""
ans = [0]*len(deck)
idxQ = deque([n for n in range(len(deck))])
# [0 1 2 3 4 5 6] idxQ
deck.sort()
# [2, 3, 5, 7, 11, 13, 17]
sortedIdx = 0
while idxQ:
idx1 = idxQ.popleft()
ans[idx1] = deck[sortedIdx]
sortedIdx += 1
if idxQ:
idx2 = idxQ.popleft()
idxQ.append(idx2)
return ans
# N: number of deck elements
# TC: O(NLogN)
# SC: O(N)
```
###### tags: `mock interview` `面試`