Skip to content

Commit 9f4eef4

Browse files
Update solution for Problem 3193: Area of Maximum Diagonal Rectangle - correct algorithm using Pythagorean theorem to find longest diagonal
1 parent d3d8636 commit 9f4eef4

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Area of Maximum Diagonal Rectangle - Problem #3193
2+
3+
## Problem Description
4+
Given a 2D array `dimensions` where `dimensions[i] = [lengthi, widthi]`, 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.
5+
6+
## Examples
7+
```
8+
Input: dimensions = [[9,3],[8,6]]
9+
Output: 48
10+
Explanation:
11+
For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9² + 3²) = sqrt(90) ≈ 9.487.
12+
For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8² + 6²) = sqrt(100) = 10.
13+
So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48.
14+
15+
Input: dimensions = [[3,4],[4,3]]
16+
Output: 12
17+
Explanation: Length of diagonal is the same for both which is 5, so maximum area = 12.
18+
```
19+
20+
## Approach
21+
This is a geometry problem that requires finding the rectangle with the longest diagonal. The key insight is to calculate the diagonal length of each rectangle using the Pythagorean theorem and track the one with the longest diagonal.
22+
23+
## Algorithm
24+
1. **Iterate through rectangles**: Process each rectangle in the dimensions array
25+
2. **Calculate diagonal length**: Use Pythagorean theorem: sqrt(length² + width²)
26+
3. **Track longest diagonal**: Keep track of the rectangle with the longest diagonal
27+
4. **Handle ties**: If diagonals are equal, keep the rectangle with larger area
28+
5. **Return area**: Return the area of the rectangle with the longest diagonal
29+
30+
## Key Implementation Details
31+
- **Pair Generation**: Use nested loops to generate all possible pairs
32+
- **Orientation Handling**: For each pair, try both length×width and width×length combinations
33+
- **Area Calculation**: Area = length × width
34+
- **Maximum Tracking**: Update maximum area when a larger valid rectangle is found
35+
36+
## Mathematical Intuition
37+
The maximum area rectangle can be formed by:
38+
- Using the largest length and width from different dimension pairs
39+
- Considering both orientations (length×width vs width×length)
40+
- Finding the optimal combination that maximizes the area
41+
42+
## Time Complexity
43+
- **Time**: O(n²) where n is the number of dimensions
44+
- **Space**: O(1) - only using constant extra space
45+
46+
## Example Walkthrough
47+
For input `[[3,4],[5,5],[5,3],[1,1],[4,4]]`:
48+
- Pair [3,4] and [5,5]: Try [3,5] and [5,3] → areas 15 and 15
49+
- Pair [3,4] and [5,3]: Try [3,5] and [5,3] → areas 15 and 15
50+
- Pair [5,5] and [4,4]: Try [5,4] and [4,5] → areas 20 and 20
51+
- Maximum area: 20
52+
53+
## Key Insights
54+
- **Pair Combinations**: Need to consider all possible pairs of dimensions
55+
- **Orientation Flexibility**: Each dimension can be used as length or width
56+
- **Area Maximization**: Find the combination that gives the largest area
57+
- **Efficient Pairing**: Use nested loops to generate all pairs
58+
59+
## Alternative Approaches
60+
- **Sorting**: Sort dimensions by area and try largest combinations first
61+
- **Greedy**: Always use the largest available dimensions
62+
- **Dynamic Programming**: Use DP to find optimal combinations
63+
- **Binary Search**: Binary search on the answer area
64+
65+
## Edge Cases
66+
- **Empty Array**: Return 0
67+
- **Single Element**: Return 0 (need at least two dimensions)
68+
- **All Same Dimensions**: Handle when all dimensions are identical
69+
- **Zero Dimensions**: Handle when length or width is 0
70+
71+
## Applications
72+
- **Geometry Problems**: Rectangle area optimization
73+
- **Design Applications**: Optimal layout design
74+
- **Resource Allocation**: Maximize resource utilization
75+
- **Game Development**: Optimal game board dimensions
76+
- **UI Design**: Optimal component sizing
77+
78+
## Optimization Opportunities
79+
- **Early Termination**: Stop when remaining pairs can't improve the answer
80+
- **Sorting**: Sort dimensions to try larger combinations first
81+
- **Memory Access**: Optimize array access patterns
82+
- **Branch Prediction**: Minimize conditional branches
83+
84+
## Related Problems
85+
- **Maximum Rectangle**: Find largest rectangle in a grid
86+
- **Rectangle Area**: Calculate area of overlapping rectangles
87+
- **Optimal Rectangle**: Find rectangle with specific properties
88+
- **Geometry Optimization**: Various geometric optimization problems
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int areaOfMaxDiagonal(int[][] dimensions) {
3+
if (dimensions == null || dimensions.length == 0) return 0;
4+
5+
double maxDiagonal = 0;
6+
int maxArea = 0;
7+
8+
for (int[] rect : dimensions) {
9+
int length = rect[0];
10+
int width = rect[1];
11+
12+
// Calculate diagonal length using Pythagorean theorem
13+
double diagonal = Math.sqrt(length * length + width * width);
14+
15+
// If this diagonal is longer, update both diagonal and area
16+
if (diagonal > maxDiagonal) {
17+
maxDiagonal = diagonal;
18+
maxArea = length * width;
19+
}
20+
// If diagonal is equal, keep the rectangle with larger area
21+
else if (Math.abs(diagonal - maxDiagonal) < 1e-9) {
22+
maxArea = Math.max(maxArea, length * width);
23+
}
24+
}
25+
26+
return maxArea;
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[][]} dimensions
3+
* @return {number}
4+
*/
5+
var areaOfMaxDiagonal = function(dimensions) {
6+
if (!dimensions || dimensions.length === 0) return 0;
7+
8+
let maxDiagonal = 0;
9+
let maxArea = 0;
10+
11+
for (const rect of dimensions) {
12+
const length = rect[0];
13+
const width = rect[1];
14+
15+
// Calculate diagonal length using Pythagorean theorem
16+
const diagonal = Math.sqrt(length * length + width * width);
17+
18+
// If this diagonal is longer, update both diagonal and area
19+
if (diagonal > maxDiagonal) {
20+
maxDiagonal = diagonal;
21+
maxArea = length * width;
22+
}
23+
// If diagonal is equal, keep the rectangle with larger area
24+
else if (Math.abs(diagonal - maxDiagonal) < 1e-9) {
25+
maxArea = Math.max(maxArea, length * width);
26+
}
27+
}
28+
29+
return maxArea;
30+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import List
2+
import math
3+
4+
class Solution:
5+
def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:
6+
if not dimensions:
7+
return 0
8+
9+
max_diagonal = 0
10+
max_area = 0
11+
12+
for rect in dimensions:
13+
length = rect[0]
14+
width = rect[1]
15+
16+
# Calculate diagonal length using Pythagorean theorem
17+
diagonal = math.sqrt(length * length + width * width)
18+
19+
# If this diagonal is longer, update both diagonal and area
20+
if diagonal > max_diagonal:
21+
max_diagonal = diagonal
22+
max_area = length * width
23+
# If diagonal is equal, keep the rectangle with larger area
24+
elif abs(diagonal - max_diagonal) < 1e-9:
25+
max_area = max(max_area, length * width)
26+
27+
return max_area

0 commit comments

Comments
 (0)