LeetCode solutions

2333. Count Number of Rectangles Containing Each Point

My accepted Python solution to LeetCode problem 2333, Count Number of Rectangles Containing Each Point, running in 783ms.

  • Difficulty: Medium
  • Python
  • Runtime 783ms
  • Memory 35.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 783ms, memory 35.4MB, accepted 2026-01-02.

python
class Solution:
    def countRectangles(self, rectangles: List[List[int]], points: List[List[int]]) -> List[int]:
        from collections import defaultdict
        from bisect import bisect_left
        
        # Group rectangles by height (y-coordinate is small, max 100)
        height_to_x = defaultdict(list)
        for x, y in rectangles:
            height_to_x[y].append(x)
        
        # Sort each list by x
        for y in height_to_x:
            height_to_x[y].sort()
        
        result = []
        for px, py in points:
            count = 0
            # For each height >= py, count rectangles with x >= px
            for y in range(py, 101):
                if y in height_to_x:
                    x_list = height_to_x[y]
                    count += len(x_list) - bisect_left(x_list, px)
            result.append(count)
        
        return result

Source