LeetCode 2250: Count Number of Rectangles Containing Each Point Solution

Master LeetCode problem 2250 (Count Number of Rectangles Containing Each Point), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2250. Count Number of Rectangles Containing Each Point

Problem Explanation

Explanation:

To solve this problem, we need to iterate through each point and count how many rectangles contain that point. For each point, we check if it lies within each rectangle by comparing its coordinates with the rectangle's dimensions. If the point lies within a rectangle, we increment the count for that point.

  • Algorithm:

    1. Initialize an array count of size equal to the number of points, initially filled with zeros.
    2. For each point in the points array, iterate through each rectangle in the rectangles array.
    3. Check if the point lies within the rectangle by comparing its x and y coordinates with the rectangle's dimensions.
    4. If the point lies within the rectangle, increment the count for that point.
    5. Return the count array as the final result.
  • Time Complexity: O(N*M) where N is the number of points and M is the number of rectangles.

  • Space Complexity: O(N) where N is the number of points.

:

Solution Code

class Solution {
    public int[] countPoints(int[][] rectangles, int[][] points) {
        int[] count = new int[points.length];
        
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < rectangles.length; j++) {
                if (points[i][0] >= 0 && points[i][0] <= rectangles[j][0] &&
                    points[i][1] >= 0 && points[i][1] <= rectangles[j][1]) {
                    count[i]++;
                }
            }
        }
        
        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2250 (Count Number of Rectangles Containing Each Point)?

This page provides optimized solutions for LeetCode problem 2250 (Count Number of Rectangles Containing Each Point) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2250 (Count Number of Rectangles Containing Each Point)?

The time complexity for LeetCode 2250 (Count Number of Rectangles Containing Each Point) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2250 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2250 in Java, C++, or Python.

Back to LeetCode Solutions