Skip to content
Discussion options

You must be logged in to vote

We need to efficiently track the number of distinct colors among balls after each query. Each query updates the color of a specific ball, and we need to determine the number of distinct colors present after each update.

Approach

  1. Data Structures: Use two hash maps (associative arrays in PHP):

    • colorMap to track the current color of each ball.
    • colorCount to track the number of balls for each color.
  2. Processing Queries:

    • For each query, check if the ball has a previous color. If so, decrement the count of that color in colorCount. If the count reaches zero, remove the color from colorCount.
    • Update the ball's color in colorMap and increment the count of the new color in colorCount.
    • The nu…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Feb 7, 2025
Maintainer Author

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Feb 7, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Feb 7, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants