|
| 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 <= i < 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> </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> </p> |
| 30 | +<p><strong>Constraints:</strong></p> |
| 31 | + |
| 32 | +<ul> |
| 33 | + <li><code>1 <= dimensions.length <= 100</code></li> |
| 34 | + <li><code><font face="monospace">dimensions[i].length == 2</font></code></li> |
| 35 | + <li><code><font face="monospace">1 <= dimensions[i][0], dimensions[i][1] <= 100</font></code></li> |
| 36 | +</ul> |
| 37 | +</div> |
| 38 | + |
| 39 | +<p> </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> </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 | +``` |
0 commit comments