Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: K-th Nearest Obstacle Queries

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3275" 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

There is an infinite 2D plane. You are given a positive integer k. You are also given a 2D array queries, which contains the following queries: queries[i] = [x, y]: Build an obstacle at coordinate (x, y) in the plane. It is guaranteed that there is no obstacle at this coordinate when this query is made. After each query, you need to find the distance of the kth nearest obstacle from the origin. Return an integer array results where results[i] denotes the kth nearest obstacle after query i, or results[i] == -1 if there are less than k obstacles. Note that initially there are no obstacles anywhere. The distance of an obstacle at coordinate (x, y) from the origin is given by |x| + |y|. Example 1: Input: queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2 Output: [-1,7,5,3] Explanation: Initially, there are 0 obstacles. After queries[0], there are less than 2 obstacles. After queries[1], there are obstacles at distances 3 and 7. After queries[2], there are obstacles at distances 3, 5, and 7. After queries[3], there are obstacles at distances 3, 3, 5, and 7. Example 2: Input: queries = [[5,5],[4,4],[3,3]], k = 1 Output: [10,8,6] Explanation: After queries[0], there is an obstacle at distance 10. After queries[1], there are obstacles at distances 8 and 10. After queries[2], there are obstacles at distances 6, 8, and 10. Constraints: 1 <= queries.length <= 2 * 105 All queries[i] are unique. -109 <= queries[i][0], queries[i][1] <= 109 1 <= k <= 105

Explanation

Here's the approach, complexity analysis, and Python code for the problem:

  • Maintain a sorted list of obstacle distances: We will store the distances of all obstacles from the origin in a sorted list. This allows efficient retrieval of the k-th smallest distance.
  • Insert obstacle distances while maintaining sorted order: After each query, calculate the distance of the new obstacle from the origin and insert it into the sorted list while maintaining the order.
  • Return the k-th smallest distance: After each insertion, if the number of obstacles is less than k, return -1. Otherwise, return the k-th smallest distance from the sorted list.

  • Runtime Complexity: O(n * k), where n is the number of queries, due to insertion into a sorted list which effectively maintains the sorted list by going through at most k elements.

  • Storage Complexity: O(n) to store the distances.

Code

    def kth_nearest_obstacle(queries, k):
    """
    Finds the distance of the kth nearest obstacle from the origin after each query.

    Args:
        queries: A 2D array of queries, where queries[i] = [x, y].
        k: A positive integer representing the kth nearest obstacle.

    Returns:
        An integer array results where results[i] denotes the kth nearest obstacle after query i,
        or results[i] == -1 if there are less than k obstacles.
    """

    obstacles = []
    results = []

    for x, y in queries:
        distance = abs(x) + abs(y)

        # Insert the distance into the sorted list
        inserted = False
        for i in range(len(obstacles)):
            if distance < obstacles[i]:
                obstacles.insert(i, distance)
                inserted = True
                break
        if not inserted:
            obstacles.append(distance)

        # Determine the result for this query
        if len(obstacles) < k:
            results.append(-1)
        else:
            results.append(obstacles[k - 1])

    return results

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: K-th Nearest Obstacle Queries