Count Number of Rectangles Containing Each Point - Complete Solution Guide
Count Number of Rectangles Containing Each Point is LeetCode problem 2250, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Framing
Count Number of Rectangles Containing Each Point is a Medium LeetCode problem that rewards careful tracing, edge-case handling, and a clear grasp of Array and Hash Table. The best solutions usually explain why the chosen invariant holds before they optimize for time or space.
Quick Example Mindset
A useful way to test Count Number of Rectangles Containing Each Point is to start with a tiny input that exposes the boundary conditions, then run the same logic on a slightly larger case to verify the array behavior and the hash table interaction. That second pass is where off-by-one mistakes and missing updates usually appear.
Problem Statement
You are given a 2D integer array rectangles where rectangles[i] = [l i , h i ] indicates that i th rectangle has a length of l i and a height of h i . You are also given a 2D integer array points where points[j] = [x j , y j ] is a point with coordinates (x j , y j ) . The i th rectangle has its bottom-left corner point at the coordinates (0, 0) and its top-right corner point at (l i , h i ) . Return an integer array count of length points.length where count[j] is the number of rectangles that c
Detailed Explanation
The problem asks us to count, for each given point (x, y), how many rectangles from a given list of rectangles contain that point. Each rectangle is defined by its top-right corner (l, h), assuming its bottom-left corner is at (0, 0). A rectangle contains a point (x, y) if 0 <= x <= l and 0 <= y <= h. The input consists of two arrays: `rectangles` (length, height) and `points` (x, y). The output should be an array `count` where `count[i]` represents the number of rectangles containing the i-th point.
Solution Approach
The solution uses a combination of bucket sort (based on the height of rectangles and y-coordinate of points) and binary search. First, rectangles are grouped by their heights into a list of lists. Then each list is sorted by the length of the rectangles. Points are also grouped by their y-coordinates. We iterate through the heights from 100 down to 1. At each height, we merge the lengths of the rectangles at that height with a list of rectangle lengths encountered so far. Finally, for each point with y-coordinate equal to the current height, we use binary search to determine the number of rectangle lengths in our lengths_so_far that are greater than or equal to the point's x-coordinate. This counts the number of rectangles containing the point. The result for each point is stored in the output array.
Step-by-Step Algorithm
- Step 1: Create a list of lists, `rects_by_height`, to store rectangles grouped by their heights. Iterate through the `rectangles` array, placing the length of each rectangle into the list corresponding to its height.
- Step 2: Sort the lengths of the rectangles within each height's list in `rects_by_height` in ascending order.
- Step 3: Create a list of lists, `points_by_y`, to store points grouped by their y-coordinates. Store each point's x-coordinate and its original index within the points array.
- Step 4: Initialize the `ans` array, which will store the result for each point.
- Step 5: Initialize lengths_so_far, which store the rectangle lengths from heights greater than or equal to current height, in sorted order.
- Step 6: Iterate through the heights from 100 down to 1. For each height:
- Step 7: Merge new_lengths (rects at current height) with lengths_so_far into lengths_so_far maintaining sorted order
- Step 8: Iterate through the points with y-coordinate equal to the current height. For each point, use binary search in lengths_so_far to find the index of the first length that is greater than or equal to the point's x-coordinate.
- Step 9: The number of rectangles containing the point is the number of lengths in lengths_so_far that are greater than or equal to the point's x-coordinate, which is the length of lengths_so_far minus the index found in the previous step. Store this value in the `ans` array at the original index of the point.
- Step 10: Return the `ans` array.
Key Insights
- Insight 1: Sorting rectangles by height and using binary search can efficiently determine how many rectangles contain a point.
- Insight 2: Processing points and rectangles by height, from highest to lowest, enables us to maintain a sorted list of rectangle lengths encountered so far, and quickly query how many of them contain a given point's x-coordinate.
- Insight 3: The height constraint (1 <= h_i, y_j <= 100) allows for the use of bucket sort (or counting sort) for grouping rectangles and points by their y-coordinates which improves performance.
Complexity Analysis
Time Complexity: O(m*n + m*log(m) + p*m)
Space Complexity: O(m + p)
Topics
This problem involves: Array, Hash Table, Binary Search, Binary Indexed Tree, Sorting.
Study Paths
Continue from this problem into the surrounding topic and company clusters to compare how the same pattern appears in other interview settings.
Related topics: Array, Hash Table, Binary Search, Binary Indexed Tree
Frequently Asked Questions
What is the most efficient way to solve array problems?
Most array problems can be solved efficiently using techniques like two-pointer approach, sliding window, or hash maps. The key is identifying whether the array is sorted (enabling binary search) or if tracking seen elements (using hash maps) can help achieve O(n) time complexity.
What are the trade-offs of using a hash table?
Hash tables provide O(1) average-case lookup, insertion, and deletion, making them ideal for frequency counting and duplicate detection. However, they use O(n) extra space and may degrade to O(n) time in worst-case scenarios with hash collisions. Always consider if the space trade-off is acceptable.
What techniques are commonly tested in medium-difficulty problems?
Medium problems often require combining multiple techniques: two-pointer with hash maps, BFS/DFS with state tracking, dynamic programming with optimization, or divide-and-conquer approaches. They test your ability to recognize patterns and choose the right combination of techniques.