LeetCode 3380: Maximum Area Rectangle With Point Constraints I Solution
Master LeetCode problem 3380 (Maximum Area Rectangle With Point Constraints I), 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.
3380. Maximum Area Rectangle With Point Constraints I
Problem Explanation
Explanation:
To solve this problem, we can iterate through all possible pairs of points and calculate the Manhattan distance between them. If the Manhattan distance is equal to the diagonal of the rectangle formed by those two points, we can calculate the area of the rectangle and keep track of the maximum area found so far. We need to ensure that no other point lies inside or on the border of the rectangle.
-
Algorithm:
- Iterate through all pairs of points.
- For each pair of points, calculate the Manhattan distance.
- If the Manhattan distance is equal to the diagonal of the rectangle, check if there is no other point inside or on the border.
- Calculate the area of the rectangle and update the maximum area found so far.
- Return the maximum area or -1 if no valid rectangle is found.
-
Time Complexity: O(n^2) where n is the number of points.
-
Space Complexity: O(1)
:
Solution Code
class Solution {
public int maxArea(int[][] points) {
int maxArea = -1;
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
int manhattanDist = Math.abs(points[i][0] - points[j][0]) + Math.abs(points[i][1] - points[j][1]);
int area = manhattanDist * manhattanDist;
boolean validRectangle = true;
for (int[] point : points) {
if (point != points[i] && point != points[j] &&
point[0] >= Math.min(points[i][0], points[j][0]) &&
point[0] <= Math.max(points[i][0], points[j][0]) &&
point[1] >= Math.min(points[i][1], points[j][1]) &&
point[1] <= Math.max(points[i][1], points[j][1])) {
validRectangle = false;
break;
}
}
if (validRectangle) {
maxArea = Math.max(maxArea, area);
}
}
}
return maxArea;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3380 (Maximum Area Rectangle With Point Constraints I)?
This page provides optimized solutions for LeetCode problem 3380 (Maximum Area Rectangle With Point Constraints I) 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 3380 (Maximum Area Rectangle With Point Constraints I)?
The time complexity for LeetCode 3380 (Maximum Area Rectangle With Point Constraints I) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3380 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3380 in Java, C++, or Python.