Skip to content

Commit d56637d

Browse files
committed
feat: add solutions to lc problem: No.3000
1 parent ee147f7 commit d56637d

File tree

4 files changed

+141
-3
lines changed

4 files changed

+141
-3
lines changed

solution/3000-3099/3000.Maximum Area of Longest Diagonal Rectangle/README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ tags:
6161

6262
<!-- solution:start -->
6363

64-
### 方法一
64+
### 方法一:数学
65+
66+
根据勾股定理,矩形的对角线的平方为 $l^2 + w^2$,其中 $l$ 和 $w$ 分别是矩形的长度和宽度。
67+
68+
我们可以遍历所有矩形,计算它们的对角线长度的平方,并记录下最大的对角线长度和对应的面积。
69+
70+
遍历结束后,我们返回记录的最大面积。
71+
72+
时间复杂度 $O(n)$,其中 $n$ 是矩形的数量。空间复杂度 $O(1)$。
6573

6674
<!-- tabs:start -->
6775

@@ -161,6 +169,50 @@ function areaOfMaxDiagonal(dimensions: number[][]): number {
161169
}
162170
```
163171

172+
#### Rust
173+
174+
```rust
175+
impl Solution {
176+
pub fn area_of_max_diagonal(dimensions: Vec<Vec<i32>>) -> i32 {
177+
let mut ans = 0;
178+
let mut mx = 0;
179+
for d in dimensions {
180+
let l = d[0];
181+
let w = d[1];
182+
let t = l * l + w * w;
183+
if mx < t {
184+
mx = t;
185+
ans = l * w;
186+
} else if mx == t {
187+
ans = ans.max(l * w);
188+
}
189+
}
190+
ans
191+
}
192+
}
193+
```
194+
195+
#### C#
196+
197+
```cs
198+
public class Solution {
199+
public int AreaOfMaxDiagonal(int[][] dimensions) {
200+
int ans = 0, mx = 0;
201+
foreach (var d in dimensions) {
202+
int l = d[0], w = d[1];
203+
int t = l * l + w * w;
204+
if (mx < t) {
205+
mx = t;
206+
ans = l * w;
207+
} else if (mx == t) {
208+
ans = Math.Max(ans, l * w);
209+
}
210+
}
211+
return ans;
212+
}
213+
}
214+
```
215+
164216
<!-- tabs:end -->
165217

166218
<!-- solution:end -->

solution/3000-3099/3000.Maximum Area of Longest Diagonal Rectangle/README_EN.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ tags:
3030
<pre>
3131
<strong>Input:</strong> dimensions = [[9,3],[8,6]]
3232
<strong>Output:</strong> 48
33-
<strong>Explanation:</strong>
33+
<strong>Explanation:</strong>
3434
For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) &asymp;<!-- notionvc: 882cf44c-3b17-428e-9c65-9940810216f1 --> 9.487.
3535
For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10.
3636
So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48.
@@ -59,7 +59,15 @@ So, the rectangle at index 1 has a greater diagonal length therefore we return a
5959

6060
<!-- solution:start -->
6161

62-
### Solution 1
62+
### Solution 1: Mathematics
63+
64+
According to the Pythagorean theorem, the square of the diagonal of a rectangle is $l^2 + w^2$, where $l$ and $w$ are the length and width of the rectangle, respectively.
65+
66+
We can iterate through all the rectangles, calculate the square of their diagonal lengths, and keep track of the maximum diagonal length and the corresponding area.
67+
68+
After the iteration, we return the recorded maximum area.
69+
70+
The time complexity is $O(n)$, where $n$ is the number of rectangles. The space complexity is $O(1)$.
6371

6472
<!-- tabs:start -->
6573

@@ -159,6 +167,50 @@ function areaOfMaxDiagonal(dimensions: number[][]): number {
159167
}
160168
```
161169

170+
#### Rust
171+
172+
```rust
173+
impl Solution {
174+
pub fn area_of_max_diagonal(dimensions: Vec<Vec<i32>>) -> i32 {
175+
let mut ans = 0;
176+
let mut mx = 0;
177+
for d in dimensions {
178+
let l = d[0];
179+
let w = d[1];
180+
let t = l * l + w * w;
181+
if mx < t {
182+
mx = t;
183+
ans = l * w;
184+
} else if mx == t {
185+
ans = ans.max(l * w);
186+
}
187+
}
188+
ans
189+
}
190+
}
191+
```
192+
193+
#### C#
194+
195+
```cs
196+
public class Solution {
197+
public int AreaOfMaxDiagonal(int[][] dimensions) {
198+
int ans = 0, mx = 0;
199+
foreach (var d in dimensions) {
200+
int l = d[0], w = d[1];
201+
int t = l * l + w * w;
202+
if (mx < t) {
203+
mx = t;
204+
ans = l * w;
205+
} else if (mx == t) {
206+
ans = Math.Max(ans, l * w);
207+
}
208+
}
209+
return ans;
210+
}
211+
}
212+
```
213+
162214
<!-- tabs:end -->
163215

164216
<!-- solution:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Solution {
2+
public int AreaOfMaxDiagonal(int[][] dimensions) {
3+
int ans = 0, mx = 0;
4+
foreach (var d in dimensions) {
5+
int l = d[0], w = d[1];
6+
int t = l * l + w * w;
7+
if (mx < t) {
8+
mx = t;
9+
ans = l * w;
10+
} else if (mx == t) {
11+
ans = Math.Max(ans, l * w);
12+
}
13+
}
14+
return ans;
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn area_of_max_diagonal(dimensions: Vec<Vec<i32>>) -> i32 {
3+
let mut ans = 0;
4+
let mut mx = 0;
5+
for d in dimensions {
6+
let l = d[0];
7+
let w = d[1];
8+
let t = l * l + w * w;
9+
if mx < t {
10+
mx = t;
11+
ans = l * w;
12+
} else if mx == t {
13+
ans = ans.max(l * w);
14+
}
15+
}
16+
ans
17+
}
18+
}

0 commit comments

Comments
 (0)