-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsolution.py
45 lines (43 loc) · 1.22 KB
/
solution.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
class Solution(object):
def isNStraightHand(self, hand, W):
"""
:type hand: List[int]
:type W: int
:rtype: bool
"""
length = len(hand)
if length % W: return False
K = length / W
hand.sort()
while K:
tmp = []
handcp = hand[:]
for idx, card in enumerate(handcp):
if len(tmp) < W:
if not tmp or card == tmp[-1] + 1:
tmp.append(card)
hand.remove(card)
else: break
if len(tmp) < W: return False
K -= 1
return not hand
#####################################
from collections import Counter
class Solution(object):
def isNStraightHand(self, hand, W):
"""
:type hand: List[int]
:type W: int
:rtype: bool
"""
hand = Counter(hand)
while hand:
start = min(hand)
for card in xrange(start, start+W):
if not hand[card]:
return False
elif hand[card] == 1:
del hand[card]
else:
hand[card] -= 1
return True