File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ fun isNStraightHand (hand : IntArray , groupSize : Int ): Boolean {
3
+ if (hand.size % groupSize != 0 ) return false
4
+ val countMap = mutableMapOf<Int , Int >()
5
+ hand.forEach { countMap[it] = countMap.getOrDefault(it, 0 ) + 1 }
6
+ val minHeap = PriorityQueue (countMap.keys)
7
+
8
+ while (minHeap.isNotEmpty()) {
9
+ val minValue = minHeap.peek()
10
+ if (countMap.getValue(minValue) == 0 ) {
11
+ minHeap.remove()
12
+ continue
13
+ }
14
+ // loop through consecutive numbers starting from the "minValue" number
15
+ for (consecutiveNumber in minValue until (minValue + groupSize)) {
16
+ if (
17
+ consecutiveNumber !in countMap.keys ||
18
+ countMap.getValue(consecutiveNumber) == 0
19
+ ) return false
20
+ countMap[consecutiveNumber] = countMap.getValue(consecutiveNumber) - 1
21
+ }
22
+ // if the loop successfully executes without returning, it indicates that
23
+ // it was possible to create a group of size [groupSize] with minValue
24
+ // as the first element in the group.
25
+ }
26
+ return true
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments