1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-14 01:49:41 +01:00
LeetCode/problems/kt/split-array-into-consecutive-subsequences.kt
Matej Focko 333866d1bc
chore: split solutions by language
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-06-02 17:19:02 +02:00

37 lines
933 B
Kotlin

class Solution {
private fun getFrequencies(nums: IntArray): MutableMap<Int, Int> {
val freqs = mutableMapOf<Int, Int>()
nums.forEach {
freqs[it] = 1 + freqs.getOrDefault(it, 0)
}
return freqs
}
fun isPossible(nums: IntArray): Boolean {
val frequencies = getFrequencies(nums)
val sortedNumbers = frequencies.keys.toList().sorted()
sortedNumbers.forEach {
while (frequencies[it]!! > 0) {
var last = 0
var j = it
var k = 0
while (frequencies.getOrDefault(j, 0) >= last) {
last = frequencies[j]!!
frequencies[j] = frequencies[j]!! - 1
j++
k++
}
if (k < 3) {
return false
}
}
}
return true
}
}