#3000
Easy Algorithms Maximum area of longest diagonal rectangle
Array
45.9% acceptance
Feb 25, 2026
499
42
You are given a 2D 0-indexed integer array dimensions.
For all indices i, 0 <= i < dimensions.length, dimensions[i][0] represents the length and dimensions[i][1] represents the width of the rectangle i.
Return the area of the rectangle having the longest diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the maximum area.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn area_of_max_diagonal(dimensions: Vec<Vec<i32>>) -> i32 {
let mut best_diag = 0i64;
let mut best_area = 0i32;
for d in &dimensions {
let (l, w) = (d[0] as i64, d[1] as i64);
let diag_sq = l * l + w * w;
let area = (l * w) as i32;
if diag_sq > best_diag || (diag_sq == best_diag && area > best_area) {
best_diag = diag_sq;
best_area = area;
}
}
best_area
}
}