Skip to content

Commit

Permalink
finish 0846
Browse files Browse the repository at this point in the history
  • Loading branch information
BLZbanme committed Dec 30, 2021
1 parent d48fdb8 commit 653375e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions 0801-0900/0801-0850/0846HandOfStraights/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from typing import List
# class Solution:
# def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
# hand.sort()
# maxVal = max(hand)
# dp = [0] * (maxVal + 1)
# for item in hand:
# dp[item] += 1
# for i in range(0, maxVal + 1):
# if not dp[i]:
# continue
# else:
# while dp[i]:
# if i + groupSize - 1 > maxVal:
# return False
# for j in range(groupSize):
# if not dp[i + j]:
# return False
# else:
# dp[i + j] -= 1
# return True

class Solution:
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
hand.sort()
maxVal = max(hand)
cnt = Counter(hand)
for item in hand:
if cnt.get(item):
while cnt.get(item):
if item + groupSize - 1 > maxVal:
return False
for j in range(groupSize):
if not cnt[item + j]:
return False
else:
cnt[item + j] -= 1
return True

print(Solution().isNStraightHand([1,2,3,6,2,3,4,7,8], 3))
print(Solution().isNStraightHand([1,2,3,4,5], 4))

0 comments on commit 653375e

Please sign in to comment.