Skip to content

Commit 5e14e12

Browse files
committed
add Kotlin solution for 846. Hand of Straights
1 parent 1d8e1bf commit 5e14e12

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

kotlin/0846-hand-of-straights.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

0 commit comments

Comments
 (0)