Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Find the Number of Distinct Colors Among the Balls

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3160" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given an integer limit and a 2D array queries of size n x 2. There are limit + 1 balls with distinct labels in the range [0, limit]. Initially, all balls are uncolored. For every query in queries that is of the form [x, y], you mark ball x with the color y. After each query, you need to find the number of colors among the balls. Return an array result of length n, where result[i] denotes the number of colors after ith query. Note that when answering a query, lack of a color will not be considered as a color. Example 1: Input: limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]] Output: [1,2,2,3] Explanation: After query 0, ball 1 has color 4. After query 1, ball 1 has color 4, and ball 2 has color 5. After query 2, ball 1 has color 3, and ball 2 has color 5. After query 3, ball 1 has color 3, ball 2 has color 5, and ball 3 has color 4. Example 2: Input: limit = 4, queries = [[0,1],[1,2],[2,2],[3,4],[4,5]] Output: [1,2,2,3,4] Explanation: After query 0, ball 0 has color 1. After query 1, ball 0 has color 1, and ball 1 has color 2. After query 2, ball 0 has color 1, and balls 1 and 2 have color 2. After query 3, ball 0 has color 1, balls 1 and 2 have color 2, and ball 3 has color 4. After query 4, ball 0 has color 1, balls 1 and 2 have color 2, ball 3 has color 4, and ball 4 has color 5. Constraints: 1 <= limit <= 109 1 <= n == queries.length <= 105 queries[i].length == 2 0 <= queries[i][0] <= limit 1 <= queries[i][1] <= 109

Explanation

  • Use a hash map (dictionary) to store the color of each ball. This allows for efficient lookups and updates of ball colors.
    • Maintain a set to track the distinct colors. This enables easy calculation of the number of colors after each query.
    • Iterate through the queries, updating the color of the corresponding ball and updating the set of distinct colors.
  • Runtime Complexity: O(n), where n is the number of queries. Storage Complexity: O(n), in the worst case where all balls have distinct colors.

Code

    def colored_balls(limit: int, queries: list[list[int]]) -> list[int]:
    """
    Given an integer limit and a 2D array queries of size n x 2. There are limit + 1 balls with distinct labels in the range [0, limit].
    Initially, all balls are uncolored. For every query in queries that is of the form [x, y], you mark ball x with the color y.
    After each query, you need to find the number of colors among the balls. Return an array result of length n, where result[i] denotes the number of colors after ith query.
    Note that when answering a query, lack of a color will not be considered as a color.

    For example:
    colored_balls(4, [[1,4],[2,5],[1,3],[3,4]]) == [1, 2, 2, 3]
    colored_balls(4, [[0,1],[1,2],[2,2],[3,4],[4,5]]) == [1, 2, 2, 3, 4]
    """

    ball_colors = {}  # Dictionary to store the color of each ball
    distinct_colors = set()  # Set to store the distinct colors
    result = []

    for x, y in queries:
        ball_colors[x] = y  # Update the color of ball x
        distinct_colors = set(ball_colors.values())  # Recalculate distinct colors
        result.append(len(distinct_colors))  # Add the number of distinct colors to the result

    return result

More from this blog

C

Chatmagic blog

2894 posts