EasyNo. 3000
Maximum Area of Longest Diagonal Rectangle — LeetCode 3000 Python Solution
- Topics
- Array
- 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
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
- Time
- O(n)where n is the number of rectangles
- Space · auxiliary
- O(1)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0057 | Insert Interval | Medium |
| 0228 | Summary Ranges | Easy |
| 0485 | Max Consecutive Ones | Easy |
| 0665 | Non-decreasing Array | Medium |
| 0674 | Longest Continuous Increasing Subsequence | Easy |
| 0699 | Falling Squares | Hard |
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.