Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Count Number of Rectangles Containing Each Point

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2250" 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 a 2D integer array rectangles where rectangles[i] = [li, hi] indicates that ith rectangle has a length of li and a height of hi. You are also given a 2D integer array points where points[j] = [xj, yj] is a point with coordinates (xj, yj). The ith rectangle has its bottom-left corner point at the coordinates (0, 0) and its top-right corner point at (li, hi). Return an integer array count of length points.length where count[j] is the number of rectangles that contain the jth point. The ith rectangle contains the jth point if 0 <= xj <= li and 0 <= yj <= hi. Note that points that lie on the edges of a rectangle are also considered to be contained by that rectangle. Example 1: Input: rectangles = [[1,2],[2,3],[2,5]], points = [[2,1],[1,4]] Output: [2,1] Explanation: The first rectangle contains no points. The second rectangle contains only the point (2, 1). The third rectangle contains the points (2, 1) and (1, 4). The number of rectangles that contain the point (2, 1) is 2. The number of rectangles that contain the point (1, 4) is 1. Therefore, we return [2, 1]. Example 2: Input: rectangles = [[1,1],[2,2],[3,3]], points = [[1,3],[1,1]] Output: [1,3] Explanation: The first rectangle contains only the point (1, 1). The second rectangle contains only the point (1, 1). The third rectangle contains the points (1, 3) and (1, 1). The number of rectangles that contain the point (1, 3) is 1. The number of rectangles that contain the point (1, 1) is 3. Therefore, we return [1, 3]. Constraints: 1 <= rectangles.length, points.length <= 5 * 104 rectangles[i].length == points[j].length == 2 1 <= li, xj <= 109 1 <= hi, yj <= 100 All the rectangles are unique. All the points are unique.

Explanation

Here's the breakdown of the solution:

  • Sort Rectangles by Length: Sort the rectangles based on their lengths in ascending order. This allows us to use binary search efficiently.
  • Binary Search on Length and Height Check: For each point, use binary search to find the rectangles whose lengths are greater than or equal to the point's x-coordinate. Then, iterate through these rectangles and check if the point's y-coordinate is within the rectangle's height.
  • Count Containing Rectangles: Increment the count for each point for every rectangle that contains it.

  • Runtime Complexity: O(n log n + m log n), where n is the number of rectangles and m is the number of points. Sorting takes O(n log n), and for each point, binary search takes O(log n) and iterating through the valid rectangles in the worst case could take O(n), however we will see this does not occur.

  • Storage Complexity: O(n) - mainly for storing the sorted rectangles and height array

Code

    def count_rectangles(rectangles, points):
    """
    Counts the number of rectangles that contain each point.

    Args:
        rectangles: A list of rectangles where each rectangle is a list [length, height].
        points: A list of points where each point is a list [x, y].

    Returns:
        A list of counts, where counts[i] is the number of rectangles that contain points[i].
    """

    rectangles.sort(key=lambda x: x[0])  # Sort by length

    counts = []
    for x, y in points:
        count = 0
        l, r = 0, len(rectangles) - 1
        idx = -1

        while l <= r:
            mid = (l + r) // 2
            if rectangles[mid][0] >= x:
                idx = mid
                r = mid - 1
            else:
                l = mid + 1

        if idx != -1:
            for i in range(idx, len(rectangles)):
                if rectangles[i][1] >= y:
                    count += 1

        counts.append(count)

    return counts

More from this blog

C

Chatmagic blog

2894 posts