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:
- Initialize an array
countof size equal to the number of points, initially filled with zeros. - For each point in the
pointsarray, iterate through each rectangle in therectanglesarray. - Check if the point lies within the rectangle by comparing its x and y coordinates with the rectangle's dimensions.
- If the point lies within the rectangle, increment the count for that point.
- Return the
countarray as the final result.
- Initialize an array
-
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.