Maximum Area of Longest Diagonal Rectangle — LeetCode 3000 Python Solution
EasyArray
- Problem
- #3000
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- dimensions = [[9,3],[8,6]]
- Output
- 48
- Explanation
- For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487.
Python solution
Python
class Solution:
def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:
ans = mx = 0
for l, w in dimensions:
t = l**2 + w**2
if mx < t:
mx = t
ans = l * w
elif mx == t:
ans = max(ans, l * w)
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the number of rectangles |
| Space | O(1) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 3000. Maximum Area of Longest Diagonal Rectangle?
- LeetCode 3000. Maximum Area of Longest Diagonal Rectangle is rated Easy on LeetCode.
- What is the time complexity of LeetCode 3000. Maximum Area of Longest Diagonal Rectangle?
- The Python solution on this page runs in O(n), where n is the number of rectangles.
- What is the space complexity of LeetCode 3000. Maximum Area of Longest Diagonal Rectangle?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 3000. Maximum Area of Longest Diagonal Rectangle cover?
- LeetCode 3000. Maximum Area of Longest Diagonal Rectangle is tagged Array on LeetCode.