forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0846-hand-of-straights.kt
28 lines (27 loc) · 1.18 KB
/
0846-hand-of-straights.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
fun isNStraightHand(hand: IntArray, groupSize: Int): Boolean {
if (hand.size % groupSize != 0) return false
val countMap = mutableMapOf<Int, Int>()
hand.forEach { countMap[it] = countMap.getOrDefault(it, 0) + 1 }
val minHeap = PriorityQueue(countMap.keys)
while (minHeap.isNotEmpty()) {
val minValue = minHeap.peek()
if (countMap.getValue(minValue) == 0) {
minHeap.remove()
continue
}
// loop through consecutive numbers starting from the "minValue" number
for (consecutiveNumber in minValue until (minValue + groupSize)) {
if (
consecutiveNumber !in countMap.keys ||
countMap.getValue(consecutiveNumber) == 0
) return false
countMap[consecutiveNumber] = countMap.getValue(consecutiveNumber) - 1
}
// if the loop successfully executes without returning, it indicates that
// it was possible to create a group of size [groupSize] with minValue
// as the first element in the group.
}
return true
}
}