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:
parent
1ca11ef95b
commit
96b8b4580e
1 changed files with 28 additions and 0 deletions
28
kt/find-the-number-of-distinct-colors-among-the-balls.kt
Normal file
28
kt/find-the-number-of-distinct-colors-among-the-balls.kt
Normal 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()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue