Skip to content

3000. Maximum Area of Longest Diagonal Rectangle 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
 public:
  int areaOfMaxDiagonal(vector<vector<int>>& dimensions) {
    const vector<int> maxDimension = *ranges::max_element(
        dimensions, [](const vector<int>& a, const vector<int>& b) {
      return (a[0] * a[0] + a[1] * a[1] == b[0] * b[0] + b[1] * b[1])
                 ? (a[0] * a[1] < b[0] * b[1])
                 : (a[0] * a[0] + a[1] * a[1] < b[0] * b[0] + b[1] * b[1]);
    });
    return maxDimension[0] * maxDimension[1];
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
  public int areaOfMaxDiagonal(int[][] dimensions) {
    int[] maxDimension = Arrays.stream(dimensions)
                             .max(Comparator.comparingInt((int[] d) -> d[0] * d[0] + d[1] * d[1])
                                      .thenComparingInt((int[] d) -> d[0] * d[1]))

                             .orElseThrow();
    return maxDimension[0] * maxDimension[1];
  }
}
1
2
3
4
class Solution:
  def areaOfMaxDiagonal(self, dimensions: list[list[int]]) -> int:
    a, b = max(dimensions, key=lambda x: (x[0]**2 + x[1]**2, x[0] * x[1]))
    return a * b