#846
Hand of Straights
medium· Greedyruns: 0Alice has some number of cards and wants to rearrange them into groups, where each group has size groupSize and consists of groupSize consecutive cards. Given an integer array hand where hand[i] is the value of the ith card and an integer groupSize, return true if she can rearrange the cards as required.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 685
import heapq
from collections import Counter
class Solution:
def isNStraightHand(
self, hand: list[int], groupSize: int
) -> bool:
if len(hand) % groupSize:
return False
count = Counter(hand)
heap = list(count.keys())
heapq.heapify(heap)
while heap:
smallest = heap[0]
for num in range(smallest, smallest + groupSize):
if count[num] == 0:
return False
count[num] -= 1
if count[num] == 0:
if num != heap[0]:
return False
heapq.heappop(heap)
return True
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.