2250. Count Number of Rectangles Containing Each Point

Leetcode: 2250. Count Number of Rectangles Containing Each Point

Difficulty: Medium

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 * 10^4
  • rectangles[i].length == points[j].length == 2
  • 1 <= li, xj <= 10^9
  • 1 <= hi, yj <= 100
  • All the rectangles are unique.
  • All the points are unique.

Solution

Notice that the max height of rectangles is 100.

  • Put all the rectangles with same height into one bucket.
  • For each point (x, y), only the rectangles in buckets [y, 100] can include this point.
    • For each bucket[i] in range [y, 100], we need to find the number of rectangles that satisfies rect.x >= point.x, denoted by cnt. This cnt will contribute to total number of rectangle that including current point.
    • Sum up all the cnt above.
class Solution {
public:
    const int HMAX = 100;
    vector<int> countRectangles(vector<vector<int>>& rects, vector<vector<int>>& points) {
        vector<vector<int>> vec(HMAX + 1);
        for (auto &r : rects)
        {
            int x = r[0], y = r[1];
            vec[y].emplace_back(x);
        }
        for (auto &v : vec)
            sort(begin(v), end(v));
        
        int n = points.size();
        vector<int> res(n, 0);
        for (int i = 0; i < n; ++i)
        {
            int x = points[i][0], y = points[i][1];
            for (int h = y; h <= HMAX; ++h)
            {
                auto itor = lower_bound(begin(vec[h]), end(vec[h]), x);
                res[i] += std::distance(itor, end(vec[h]));
            }
        }
        return res;
    }
};
posted @ 2022-04-24 14:59  sinkinben  阅读(273)  评论(0编辑  收藏  举报