Maximum Area of Longest Diagonal Rectangle (LeetCode 3000): Solution and Explanation
Among a list of rectangles, find the one with the longest diagonal and return its area. If several share that diagonal, return the largest of their areas.
Intuition
By Pythagoras, the diagonal of an l by w rectangle is sqrt(l*l + w*w). The square root keeps the order of its input, so comparing l*l + w*w is enough and avoids floating point.
Picture every rectangle placed with one corner at the same point. The diagonal is the distance from that point to the far corner, so the winner is the rectangle whose far corner reaches the farthest.
Draw an arc through the far corner of the best rectangle so far. A new rectangle wins if its corner pokes outside the arc. If the corner lands exactly on the arc, it is a tie, and the larger area wins.
A long thin rectangle can beat a square of much bigger area. The diagonal decides first; area only breaks ties.
Approach: One pass on squared diagonals
- Set
best_diag = 0andbest_area = 0. - For each rectangle compute
diag = l * l + w * wandarea = l * w. - If
diag > best_diag, ordiag == best_diagandarea > best_area, store both values. - Return
best_area.
Step-by-step example
dimensions = [[9,3],[8,6]]
- Rectangle 0 is 9 x 3: diag = 81 + 9 = 90, area = 27. It is the first, so best_diag = 90 and best_area = 27.
- Rectangle 1 is 8 x 6: diag = 64 + 36 = 100, area = 48. 100 > 90, so best_diag = 100 and best_area = 48.
- No rectangles are left. Return 48.
Code: Python, Java, C++
1class Solution:
2 def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:
3 best_diag = 0
4 best_area = 0
5 for l, w in dimensions:
6 diag = l * l + w * w
7 area = l * w
8 if diag > best_diag or (diag == best_diag and area > best_area):
9 best_diag = diag
10 best_area = area
11 return best_area1class Solution {
2 public int areaOfMaxDiagonal(int[][] dimensions) {
3 int bestDiag = 0;
4 int bestArea = 0;
5 for (int[] d : dimensions) {
6 int diag = d[0] * d[0] + d[1] * d[1];
7 int area = d[0] * d[1];
8 if (diag > bestDiag || (diag == bestDiag && area > bestArea)) {
9 bestDiag = diag;
10 bestArea = area;
11 }
12 }
13 return bestArea;
14 }
15}1class Solution {
2public:
3 int areaOfMaxDiagonal(vector<vector<int>>& dimensions) {
4 int bestDiag = 0;
5 int bestArea = 0;
6 for (auto& d : dimensions) {
7 int diag = d[0] * d[0] + d[1] * d[1];
8 int area = d[0] * d[1];
9 if (diag > bestDiag || (diag == bestDiag && area > bestArea)) {
10 bestDiag = diag;
11 bestArea = area;
12 }
13 }
14 return bestArea;
15 }
16};Time and space complexity
O(n)
Each rectangle is examined once with constant work.
O(1)
Only the best squared diagonal and its area are stored.
Other approaches
Max with a tuple key (time O(n), space O(1))
Take the maximum of (l*l + w*w, l*w) over all rectangles and return its second part. Tuples compare by diagonal first and area second, which is exactly the rule.
Sort by key (time O(n log n), space O(n))
Sort the rectangles by the same tuple and take the last one. Correct, but it sorts when a single pass is enough.
Common mistakes
- Comparing
sqrtvalues as floats. Equal diagonals such as 3 x 4 and 4 x 3 can be judged unequal after rounding in some languages. Squared integers are exact. - Returning the largest area overall instead of the area of the longest diagonal. In [[6,6],[2,9]] the 2 x 9 rectangle wins with area 18, even though 6 x 6 has area 36.
- Forgetting the tie-break, which keeps the first rectangle with the longest diagonal instead of the one with the largest area.
- Updating only
best_areaand notbest_diag, so later comparisons use a stale diagonal.
Frequently asked questions
Why is it safe to skip the square root?
The square root is increasing, so a > b exactly when sqrt(a) > sqrt(b). Comparing the squared lengths gives the same winner.
Can the squared diagonal overflow a 32-bit integer?
No. Sides are at most 100, so l*l + w*w is at most 20,000.
When do two rectangles tie on the diagonal?
When l*l + w*w is the same, such as 3 x 4 and 4 x 3, or 1 x 7, 7 x 1 and 5 x 5, which all give 50.