Skip to content

Commit 81480bc

Browse files
committed
Jun 6
1 parent a25db9e commit 81480bc

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from collections import Counter
2+
from heapq import heapify, heappop
3+
from typing import List
4+
5+
6+
class Solution:
7+
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
8+
if len(hand) % groupSize != 0:
9+
return False
10+
11+
freq = Counter(hand)
12+
heapify(hand)
13+
while hand:
14+
start = heappop(hand)
15+
if freq[start] == 0:
16+
# already used
17+
continue
18+
freq[start] -= 1
19+
# check k consecutive
20+
for i in range(1, groupSize):
21+
if freq[start+i] == 0:
22+
return False
23+
freq[start+i] -= 1
24+
return True
25+
26+
27+
def main():
28+
hand = [1, 2, 3, 6, 2, 3, 4, 7, 8]
29+
groupSize = 3
30+
assert Solution().isNStraightHand(hand, groupSize) is True
31+
32+
hand = [1, 2, 3, 4, 5]
33+
groupSize = 4
34+
assert Solution().isNStraightHand(hand, groupSize) is False
35+
36+
37+
if __name__ == '__main__':
38+
main()

2024-06-June-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
| June 3 | [2486. Append Characters to String to Make Subsequence](https://leetcode.com/problems/append-characters-to-string-to-make-subsequence/) | Medium | Solved |
99
| June 4 | [409. Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | Easy | Solved |
1010
| June 5 | [1002. Find Common Characters](https://leetcode.com/problems/find-common-characters/) | Easy | Solved |
11-
| June 6 | []() | | |
11+
| June 6 | [846. Hand of Straights](https://leetcode.com/problems/hand-of-straights/) | Medium | Solved |
1212
| June 7 | []() | | |
1313
| June 8 | []() | | |
1414
| June 9 | []() | | |
@@ -39,5 +39,5 @@
3939
| Level | Problems | Solved | Unsolved |
4040
| --- | --- | --- | --- |
4141
| Easy | 4 | 4 | 0 |
42-
| Medium | 1 | 1 | 0 |
42+
| Medium | 2 | 2 | 0 |
4343
| Hard | 0 | 0 | 0 |

0 commit comments

Comments
 (0)