Skip to content

Commit 3a4bd73

Browse files
authored
Create Maximum Area of Longest Diagonal Rectangle.py
Signed-off-by: Erjan <erjan@users.noreply.github.com>
1 parent 90602ad commit 3a4bd73

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
You are given a 2D 0-indexed integer array dimensions.
2+
3+
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.
4+
5+
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.
6+
7+
------------------------------------------------------------------------------------------------
8+
9+
class Solution:
10+
def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:
11+
maxdiag = 0
12+
maxres = 0
13+
14+
print(dimensions)
15+
for i in range(len(dimensions)):
16+
w,h = dimensions[i]
17+
18+
19+
diag = (w*w + h*h)**0.5
20+
cur_res = w*h
21+
22+
dimensions[i].extend([diag,cur_res])
23+
24+
25+
dimensions.sort(key = lambda x: (x[2], x[3]))
26+
27+
res = dimensions[-1][3]
28+
29+
return res
30+
31+
--------------------------------------------------
32+
33+
#other solution
34+
35+
class Solution:
36+
def areaOfMaxDiagonal(self, dimensions: list[list[int]]) -> int:
37+
max_area, max_diag = 0, 0
38+
39+
for l, w in dimensions:
40+
curr_diag = l * l + w * w
41+
if curr_diag > max_diag or (curr_diag == max_diag and l * w > max_area):
42+
max_diag = curr_diag
43+
max_area = l * w
44+
45+
return max_area

0 commit comments

Comments
 (0)