g1601_1700.s1629_slowest_key.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g1601_1700.s1629_slowest_key
// #Easy #Array #String #2023_06_17_Time_200_ms_(75.00%)_Space_38.3_MB_(87.50%)
class Solution {
fun slowestKey(releaseTimes: IntArray, keysPressed: String): Char {
val map: MutableMap = HashMap()
for (i in releaseTimes.indices) {
val c = keysPressed[i]
val duration: Int = if (i == 0) {
releaseTimes[i]
} else {
releaseTimes[i] - releaseTimes[i - 1]
}
if (!map.containsKey(c)) {
map[c] = duration
} else {
val `val` = map[c]!!
if (duration > `val`) {
map[c] = duration
}
}
}
val map2: MutableMap> = HashMap()
for ((key, duration) in map) {
if (!map2.containsKey(duration)) {
map2[duration] = ArrayList()
}
map2[duration]!!.add(key)
}
var max = -1
for ((key, chars) in map2) {
chars.sort()
map2[key] = chars
max = max.coerceAtLeast(key)
}
return map2[max]!![map2[max]!!.size - 1]
}
}