-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandOfStraights.py
More file actions
27 lines (25 loc) · 809 Bytes
/
HandOfStraights.py
File metadata and controls
27 lines (25 loc) · 809 Bytes
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
# https://leetcode.com/problems/hand-of-straights/
# #map
class Solution:
def isNStraightHand(self, hand: List[int], W: int) -> bool:
length = len(hand)
if (length % W != 0):
return False
hand.sort() # O(nlogn)
dic = dict()
for v in hand: # O(n)
if (v not in dic):
dic[v] = 0
dic[v] += 1
for i in range(length): # O(n + n)
if (length - i < W):
break
if dic[hand[i]] == 0:
continue
dic[hand[i]] -= 1
for j in range(1, W):
new_val = hand[i] + j
if (new_val not in dic or dic[new_val] == 0):
return False
dic[new_val] -= 1
return True