Leetcode 3000. Maximum Area of Longest Diagonal Rectangle
The problem requires determining the maximum area of a rectangle that can be inscribed inside a grid such that the rectangle’s longest diagonal is parallel to one of the grid’s diagonals.
Here is the expected definition for the problem:
Given a grid of size `m x n`, return the maximum area of a rectangle that can be inscribed inside the grid such that one of its longest diagonals is parallel to one of the grid's diagonals.
m
and n
(the dimensions of the grid)?
1 <= m, n <= 10^5
.d
by d
:d
, the area would be maximized when the sides are floor(m/√2)
and floor(n/√2)
respectively.Here’s an approach to calculate the maximum area:
public class Solution {
public int maxInscribedRectangleArea(int m, int n) {
// Maximum possible diagonal length in the rotated grid
double maxSide = Math.min(m, n) / Math.sqrt(2);
// Maximum sides of the rectangle inscribed
int maxLength = (int) maxSide;
// Return the area as an integer
return maxLength * maxLength;
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.maxInscribedRectangleArea(3, 6)); // Example case
}
}
Time Analysis: The approach runs in O(1) time as it computes basic mathematical operations directly influenced by the input values m
and n
.
Space Analysis: The space complexity is also O(1) since no additional space allocations are required apart from a few variables.
This solution efficiently calculates the maximum area of a rectangle meeting the specified diagonal condition within the problem constraints.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?