846 - Hand Of Straights
Written on February 4, 2020
Tweet
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.
from collections import Counter
class Solution:
def isNStraightHand(self, hand: List[int], W: int) -> bool:
if W > len(hand) or len(hand) % W != 0:
return False
counter = Counter(hand)
for c in sorted(counter):
if counter[c] > 0:
curr = counter[c]
for i in range(W):
counter[c + i] -= curr
if counter[c + i] < 0:
return False
return True