Skip to content

Commit 4b1e2a2

Browse files
committed
feat: add solution 3000. Maximum Area of Longest Diagonal Rectangle
1 parent edab67b commit 4b1e2a2

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# [3000. Maximum Area of Longest Diagonal Rectangle](https://leetcode.com/problems/maximum-area-of-longest-diagonal-rectangle)
2+
3+
## Description
4+
5+
<div class="elfjS" data-track-load="description_content"><p>You are given a 2D <strong>0-indexed </strong>integer array <code>dimensions</code>.</p>
6+
7+
<p>For all indices <code>i</code>, <code>0 &lt;= i &lt; dimensions.length</code>, <code>dimensions[i][0]</code> represents the length and <code>dimensions[i][1]</code> represents the width of the rectangle<span style="font-size: 13.3333px;"> <code>i</code></span>.</p>
8+
9+
<p>Return <em>the <strong>area</strong> of the rectangle having the <strong>longest</strong> diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the <strong>maximum</strong> area.</em></p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre><strong>Input:</strong> dimensions = [[9,3],[8,6]]
15+
<strong>Output:</strong> 48
16+
<strong>Explanation:</strong>
17+
For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487.
18+
For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10.
19+
So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48.
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre><strong>Input:</strong> dimensions = [[3,4],[4,3]]
25+
<strong>Output:</strong> 12
26+
<strong>Explanation:</strong> Length of diagonal is the same for both which is 5, so maximum area = 12.
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li><code>1 &lt;= dimensions.length &lt;= 100</code></li>
34+
<li><code><font face="monospace">dimensions[i].length == 2</font></code></li>
35+
<li><code><font face="monospace">1 &lt;= dimensions[i][0], dimensions[i][1] &lt;= 100</font></code></li>
36+
</ul>
37+
</div>
38+
39+
<p>&nbsp;</p>
40+
41+
## Solutions
42+
43+
**Solution: `Brute Force`**
44+
45+
- Time complexity: <em>O(n)</em>
46+
- Space complexity: <em>O(1)</em>
47+
48+
<p>&nbsp;</p>
49+
50+
### **JavaScript**
51+
52+
```js
53+
/**
54+
* @param {number[][]} dimensions
55+
* @return {number}
56+
*/
57+
const areaOfMaxDiagonal = function (dimensions) {
58+
let maxDiagonal = 0;
59+
let result = 0;
60+
61+
for (const [length, width] of dimensions) {
62+
const diagonal = Math.hypot(length, width);
63+
64+
if (diagonal < maxDiagonal) continue;
65+
66+
const area = length * width;
67+
68+
result = diagonal === maxDiagonal ? Math.max(area, result) : area;
69+
maxDiagonal = diagonal;
70+
}
71+
72+
return result;
73+
};
74+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[][]} dimensions
3+
* @return {number}
4+
*/
5+
const areaOfMaxDiagonal = function (dimensions) {
6+
let maxDiagonal = 0;
7+
let result = 0;
8+
9+
for (const [length, width] of dimensions) {
10+
const diagonal = Math.hypot(length, width);
11+
12+
if (diagonal < maxDiagonal) continue;
13+
14+
const area = length * width;
15+
16+
result = diagonal === maxDiagonal ? Math.max(area, result) : area;
17+
maxDiagonal = diagonal;
18+
}
19+
20+
return result;
21+
};

0 commit comments

Comments
 (0)