846.一手顺子
一、模拟
class Solution:
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
p = []
heapq.heapify(p) #建堆
map = {}
for i in hand: #统计每个数字频率
map[i] = map.get(i, 0) + 1
heapq.heappush(p, i)
while p:
t = heapq.heappop(p) # 取堆顶(最小的数)
if map[t] == 0: #该数已用完,跳过
continue
for i in range(groupSize): #尝试取t ~ t+m-1
cnt = map.get(t+i, 0)
if cnt == 0: #数量为0,无法构成顺子
return False
map[t+i] = cnt-1
return True最后更新于