kt: add «3160. Find the Number of Distinct Colors Among the Balls»

URL:	https://leetcode.com/problems/find-the-number-of-distinct-colors-among-the-balls/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-02-07 10:21:05 +01:00
parent 1ca11ef95b
commit 96b8b4580e
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,28 @@
class Solution {
fun queryResults(
limit: Int,
queries: Array<IntArray>,
): IntArray {
val n = queries.size
val freqs = mutableMapOf<Int, Int>()
val balls = mutableMapOf<Int, Int>()
return queries.map { q ->
val (ball, color) = q[0] to q[1]
if (balls.containsKey(ball)) {
val currentColor = balls[ball]!!
freqs.put(currentColor, freqs.get(currentColor)!! - 1)
if (freqs[currentColor] == 0) {
freqs.remove(currentColor)
}
}
balls[ball] = color
freqs[color] = 1 + (freqs.get(color) ?: 0)
freqs.size
}.toIntArray()
}
}