-
Notifications
You must be signed in to change notification settings - Fork 43
/
reveal-cards-in-increasing-order.py
57 lines (54 loc) · 1.34 KB
/
reveal-cards-in-increasing-order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/84728767
import collections
class Solution(object):
def deckRevealedIncreasing(self, deck):
"""
:type deck: List[int]
:rtype: List[int]
"""
deck.sort()
N = len(deck)
res = [0] * N
que = collections.deque()
for i in range(N):
if que:
que.appendleft(que.pop())
que.appendleft(deck.pop())
return list(que)
# V1'
# https://www.cnblogs.com/seyjs/p/10058367.html
class Solution(object):
def deckRevealedIncreasing(self, deck):
"""
:type deck: List[int]
:rtype: List[int]
"""
deck.sort(reverse = True)
res = []
while len(deck) > 0:
v = deck.pop(0)
if len(res) == 0:
res.append(v)
else:
res.insert(0,res.pop(-1))
res.insert(0,v)
return res
# V2
# Time: O(n)
# Space: O(n)
import collections
class Solution(object):
def deckRevealedIncreasing(self, deck):
"""
:type deck: List[int]
:rtype: List[int]
"""
d = collections.deque()
deck.sort(reverse=True)
for i in deck:
if d:
d.appendleft(d.pop())
d.appendleft(i)
return list(d)