[LeetCode] Maximum Area of Longest Diagonal Rectangle

10035. Maximum Area of Longest Diagonal Rectangle

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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int areaOfMaxDiagonal(vector<vector<int>>& dimensions) {
int ma = 0, res = 0;
for(auto& d : dimensions) {
int dig = d[0] * d[0] + d[1] * d[1];
if(ma < dig) ma = dig, res = d[0] * d[1];
else if(ma == dig) res = max(res, d[0] * d[1]);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/01/07/PS/LeetCode/maximum-area-of-longest-diagonal-rectangle/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.