Maximum Area of Longest Diagonal Rectangle (LeetCode 3000): Solution and Explanation

Easy
Array

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

  1. Set best_diag = 0 and best_area = 0.
  2. For each rectangle compute diag = l * l + w * w and area = l * w.
  3. If diag > best_diag, or diag == best_diag and area > best_area, store both values.
  4. Return best_area.

Step-by-step example

dimensions = [[9,3],[8,6]]
  1. Rectangle 0 is 9 x 3: diag = 81 + 9 = 90, area = 27. It is the first, so best_diag = 90 and best_area = 27.
  2. Rectangle 1 is 8 x 6: diag = 64 + 36 = 100, area = 48. 100 > 90, so best_diag = 100 and best_area = 48.
  3. 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_area
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
One pass on squared diagonals O(n)
Max with a tuple key O(n)
Sort by key O(n log n)
Time

O(n)

Each rectangle is examined once with constant work.

Space

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 sqrt values 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_area and not best_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.