Skip to content

Commit 76eb60f

Browse files
authored
update: 添加问题“3000.对角线最长的矩形的面积”的代码和题解 (#1099)
* 3000: WA.cpp (#1098) [[6,5],[8,6],[2,10],[8,1],[9,2],[3,5],[3,5]] 20 48 * 3000: AC.cpp (#1098) - 刚刚晕了晕了 cpp - AC,100.00%,88.24% * 3000: AC.py+go+java+rust (#1098) py - AC,100.00%,82.26% go - AC,100.00%,11.11% java - AC,100.00%,33.33% rust - AC,100.00%,80.00% 中间和室友聊了一会儿哈哈 * update: 添加问题“3000.对角线最长的矩形的面积”的代码和题解 (#1099) Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 0997e94 commit 76eb60f

8 files changed

+340
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-26 21:25:10
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-26 21:32:11
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int areaOfMaxDiagonal(vector<vector<int>>& dimensions) {
14+
int ans = 0;
15+
int M = 0;
16+
for (vector<int>& d : dimensions) {
17+
int l2 = d[0] * d[0] + d[1] * d[1];
18+
if (l2 > M) {
19+
M = l2;
20+
ans = d[0] * d[1];
21+
} else if (l2 == M) {
22+
ans = max(ans, d[0] * d[1]);
23+
}
24+
}
25+
return ans;
26+
}
27+
};
28+
29+
#if defined(_WIN32) || defined(__APPLE__)
30+
/*
31+
[[6,5],[8,6],[2,10],[8,1],[9,2],[3,5],[3,5]]
32+
*/
33+
int main() {
34+
string s;
35+
while (cin >> s) {
36+
vector<vector<int>> v = stringToVectorVector(s);
37+
Solution sol;
38+
cout << sol.areaOfMaxDiagonal(v) << endl;
39+
}
40+
return 0;
41+
}
42+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-26 21:25:10
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-26 21:36:14
6+
*/
7+
package main
8+
9+
func areaOfMaxDiagonal(dimensions [][]int) (ans int) {
10+
M := 0
11+
for _, d := range dimensions {
12+
l2 := d[0] * d[0] + d[1] * d[1]
13+
if l2 > M {
14+
M = l2
15+
ans = d[0] * d[1]
16+
} else if l2 == M {
17+
ans = max(ans, d[0] * d[1])
18+
}
19+
}
20+
return
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-26 21:25:10
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-26 21:36:58
6+
*/
7+
class Solution {
8+
public int areaOfMaxDiagonal(int[][] dimensions) {
9+
int ans = 0;
10+
int M = 0;
11+
for (int[] d : dimensions) {
12+
int l2 = d[0] * d[0] + d[1] * d[1];
13+
if (l2 > M) {
14+
M = l2;
15+
ans = d[0] * d[1];
16+
} else if (l2 == M) {
17+
ans = Math.max(ans, d[0] * d[1]);
18+
}
19+
}
20+
return ans;
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-08-26 21:25:10
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-08-26 21:34:38
6+
'''
7+
from typing import List
8+
class Solution:
9+
def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:
10+
ans = M = 0
11+
for a, b in dimensions:
12+
l2 = a * a + b * b
13+
if l2 > M:
14+
M = l2
15+
ans = a * b
16+
elif l2 == M:
17+
ans = max(ans, a * b)
18+
return ans
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-26 21:25:10
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-26 21:40:59
6+
*/
7+
impl Solution {
8+
pub fn area_of_max_diagonal(dimensions: Vec<Vec<i32>>) -> i32 {
9+
let mut ans: i32 = 0;
10+
let mut m: i32 = 0;
11+
for d in dimensions.iter() {
12+
let l2: i32 = d[0] * d[0] + d[1] * d[1];
13+
if l2 > m {
14+
m = l2;
15+
ans = d[0] * d[1];
16+
} else if l2 == m {
17+
ans = ans.max(d[0] * d[1]);
18+
}
19+
}
20+
ans
21+
}
22+
}

Codes/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* @LastEditTime: 2025-08-11 22:07:08
66
*/
77
pub struct Solution;
8-
include!("0498-diagonal-traverse_20250825.rs"); // 这个fileName是会被脚本替换掉的
8+
include!("3000-maximum-area-of-longest-diagonal-rectangle.rs"); // 这个fileName是会被脚本替换掉的

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@
916916
|2974.最小数字游戏|简单|<a href="https://leetcode.cn/problems/minimum-number-game/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/12/LeetCode%202974.%E6%9C%80%E5%B0%8F%E6%95%B0%E5%AD%97%E6%B8%B8%E6%88%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140365205" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-game/solutions/2840437/letmefly-2974zui-xiao-shu-zi-you-xi-pai-ib5em/" target="_blank">LeetCode题解</a>|
917917
|2982.找出出现至少三次的最长特殊子字符串II|中等|<a href="https://leetcode.cn/problems/find-longest-special-substring-that-occurs-thrice-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/30/LeetCode%202982.%E6%89%BE%E5%87%BA%E5%87%BA%E7%8E%B0%E8%87%B3%E5%B0%91%E4%B8%89%E6%AC%A1%E7%9A%84%E6%9C%80%E9%95%BF%E7%89%B9%E6%AE%8A%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139334864" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-longest-special-substring-that-occurs-thrice-ii/solutions/2795996/letmefly-2982zhao-chu-chu-xian-zhi-shao-9ubg1/" target="_blank">LeetCode题解</a>|
918918
|2999.统计强大整数的数目|困难|<a href="https://leetcode.cn/problems/count-the-number-of-powerful-integers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/12/LeetCode%202999.%E7%BB%9F%E8%AE%A1%E5%BC%BA%E5%A4%A7%E6%95%B4%E6%95%B0%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147191672" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-the-number-of-powerful-integers/solutions/3649695/letmefly-2999tong-ji-qiang-da-zheng-shu-jnya8/" target="_blank">LeetCode题解</a>|
919+
|3000.对角线最长的矩形的面积|简单|<a href="https://leetcode.cn/problems/maximum-area-of-longest-diagonal-rectangle/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/08/26/LeetCode%203000.%E5%AF%B9%E8%A7%92%E7%BA%BF%E6%9C%80%E9%95%BF%E7%9A%84%E7%9F%A9%E5%BD%A2%E7%9A%84%E9%9D%A2%E7%A7%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/150871371" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-area-of-longest-diagonal-rectangle/solutions/3764586/letmefly-3000dui-jiao-xian-zui-chang-de-cxvkc/" target="_blank">LeetCode题解</a>|
919920
|3011.判断一个数组是否可以变为有序|中等|<a href="https://leetcode.cn/problems/find-if-array-can-be-sorted/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/13/LeetCode%203011.%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E6%95%B0%E7%BB%84%E6%98%AF%E5%90%A6%E5%8F%AF%E4%BB%A5%E5%8F%98%E4%B8%BA%E6%9C%89%E5%BA%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140391465" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-if-array-can-be-sorted/solutions/2841665/letmefly-3011pan-duan-yi-ge-shu-zu-shi-f-i9ck/" target="_blank">LeetCode题解</a>|
920921
|3019.按键变更的次数|简单|<a href="https://leetcode.cn/problems/number-of-changing-keys/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/01/07/LeetCode%203019.%E6%8C%89%E9%94%AE%E5%8F%98%E6%9B%B4%E7%9A%84%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144983704" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-changing-keys/solutions/3040949/letmefly-3019an-jian-bian-geng-de-ci-shu-pgzx/" target="_blank">LeetCode题解</a>|
921922
|3024.三角形类型|简单|<a href="https://leetcode.cn/problems/type-of-triangle/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/19/LeetCode%203024.%E4%B8%89%E8%A7%92%E5%BD%A2%E7%B1%BB%E5%9E%8B/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148061722" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/type-of-triangle/solutions/3680826/letmefly-3024san-jiao-xing-lei-xing-shou-q46h/" target="_blank">LeetCode题解</a>|
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
---
2+
title: 3000.对角线最长的矩形的面积:一次遍历
3+
date: 2025-08-26 21:41:54
4+
tags: [题解, LeetCode, 简单, 数组, 遍历, 模拟, 数学]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】3000.对角线最长的矩形的面积:一次遍历
9+
10+
力扣题目链接:[https://leetcode.cn/problems/maximum-area-of-longest-diagonal-rectangle/](https://leetcode.cn/problems/maximum-area-of-longest-diagonal-rectangle/)
11+
12+
<p>给你一个下标从<strong> 0</strong> 开始的二维整数数组 <code>dimensions</code>。</p>
13+
14+
<p>对于所有下标 <code>i</code>(<code>0 &lt;= i &lt; dimensions.length</code>),<code>dimensions[i][0]</code> 表示矩形 <span style="font-size: 13.3333px;"> <code>i</code></span> 的长度,而 <code>dimensions[i][1]</code> 表示矩形 <span style="font-size: 13.3333px;"> <code>i</code></span> 的宽度。</p>
15+
16+
<p>返回对角线最 <strong>长 </strong>的矩形的<strong> 面积 </strong>。如果存在多个对角线长度相同的矩形,返回面积最<strong> 大 </strong>的矩形的面积。</p>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong class="example">示例 1:</strong></p>
21+
22+
<pre>
23+
<strong>输入:</strong>dimensions = [[9,3],[8,6]]
24+
<strong>输出:</strong>48
25+
<strong>解释:</strong>
26+
下标 = 0,长度 = 9,宽度 = 3。对角线长度 = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈<!-- notionvc: 882cf44c-3b17-428e-9c65-9940810216f1 --> 9.487。
27+
下标 = 1,长度 = 8,宽度 = 6。对角线长度 = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10。
28+
因此,下标为 1 的矩形对角线更长,所以返回面积 = 8 * 6 = 48。
29+
</pre>
30+
31+
<p><strong class="example">示例 2:</strong></p>
32+
33+
<pre>
34+
<strong>输入:</strong>dimensions = [[3,4],[4,3]]
35+
<strong>输出:</strong>12
36+
<strong>解释:</strong>两个矩形的对角线长度相同,为 5,所以最大面积 = 12。
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong>提示:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= dimensions.length &lt;= 100</code></li>
45+
<li><code>dimensions[i].length == 2</code></li>
46+
<li><code>1 &lt;= dimensions[i][0], dimensions[i][1] &lt;= 100</code></li>
47+
</ul>
48+
49+
50+
51+
## 解题方法:遍历维护最大值
52+
53+
使用一个变量l2维护最大的对角线长度的平方(避免无意义的开根号),遍历所有矩形:
54+
55+
+ 如果当前矩形对角线长度的平方大于l2,则更新l2并直接更新答案为当前矩形的面积;
56+
+ 否则如果当前对角线长度的平方等于l2,则更新答案为当前矩形面积和答案的最大值。
57+
58+
### 时空复杂度分析
59+
60+
+ 时间复杂度$O(len(dimensions))$
61+
+ 空间复杂度$O(1)$
62+
63+
### AC代码
64+
65+
#### C++
66+
67+
```cpp
68+
/*
69+
* @Author: LetMeFly
70+
* @Date: 2025-08-26 21:25:10
71+
* @LastEditors: LetMeFly.xyz
72+
* @LastEditTime: 2025-08-26 21:32:11
73+
*/
74+
class Solution {
75+
public:
76+
int areaOfMaxDiagonal(vector<vector<int>>& dimensions) {
77+
int ans = 0;
78+
int M = 0;
79+
for (vector<int>& d : dimensions) {
80+
int l2 = d[0] * d[0] + d[1] * d[1];
81+
if (l2 > M) {
82+
M = l2;
83+
ans = d[0] * d[1];
84+
} else if (l2 == M) {
85+
ans = max(ans, d[0] * d[1]);
86+
}
87+
}
88+
return ans;
89+
}
90+
};
91+
92+
#if defined(_WIN32) || defined(__APPLE__)
93+
/*
94+
[[6,5],[8,6],[2,10],[8,1],[9,2],[3,5],[3,5]]
95+
*/
96+
int main() {
97+
string s;
98+
while (cin >> s) {
99+
vector<vector<int>> v = stringToVectorVector(s);
100+
Solution sol;
101+
cout << sol.areaOfMaxDiagonal(v) << endl;
102+
}
103+
return 0;
104+
}
105+
#endif
106+
```
107+
108+
#### Python
109+
110+
```python
111+
'''
112+
Author: LetMeFly
113+
Date: 2025-08-26 21:25:10
114+
LastEditors: LetMeFly.xyz
115+
LastEditTime: 2025-08-26 21:34:38
116+
'''
117+
from typing import List
118+
class Solution:
119+
def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:
120+
ans = M = 0
121+
for a, b in dimensions:
122+
l2 = a * a + b * b
123+
if l2 > M:
124+
M = l2
125+
ans = a * b
126+
elif l2 == M:
127+
ans = max(ans, a * b)
128+
return ans
129+
```
130+
131+
#### Java
132+
133+
```java
134+
/*
135+
* @Author: LetMeFly
136+
* @Date: 2025-08-26 21:25:10
137+
* @LastEditors: LetMeFly.xyz
138+
* @LastEditTime: 2025-08-26 21:36:58
139+
*/
140+
class Solution {
141+
public int areaOfMaxDiagonal(int[][] dimensions) {
142+
int ans = 0;
143+
int M = 0;
144+
for (int[] d : dimensions) {
145+
int l2 = d[0] * d[0] + d[1] * d[1];
146+
if (l2 > M) {
147+
M = l2;
148+
ans = d[0] * d[1];
149+
} else if (l2 == M) {
150+
ans = Math.max(ans, d[0] * d[1]);
151+
}
152+
}
153+
return ans;
154+
}
155+
}
156+
```
157+
158+
#### Go
159+
160+
```go
161+
/*
162+
* @Author: LetMeFly
163+
* @Date: 2025-08-26 21:25:10
164+
* @LastEditors: LetMeFly.xyz
165+
* @LastEditTime: 2025-08-26 21:36:14
166+
*/
167+
package main
168+
169+
func areaOfMaxDiagonal(dimensions [][]int) (ans int) {
170+
M := 0
171+
for _, d := range dimensions {
172+
l2 := d[0] * d[0] + d[1] * d[1]
173+
if l2 > M {
174+
M = l2
175+
ans = d[0] * d[1]
176+
} else if l2 == M {
177+
ans = max(ans, d[0] * d[1])
178+
}
179+
}
180+
return
181+
}
182+
```
183+
184+
#### Rust
185+
186+
```rust
187+
/*
188+
* @Author: LetMeFly
189+
* @Date: 2025-08-26 21:25:10
190+
* @LastEditors: LetMeFly.xyz
191+
* @LastEditTime: 2025-08-26 21:40:59
192+
*/
193+
impl Solution {
194+
pub fn area_of_max_diagonal(dimensions: Vec<Vec<i32>>) -> i32 {
195+
let mut ans: i32 = 0;
196+
let mut m: i32 = 0;
197+
for d in dimensions.iter() {
198+
let l2: i32 = d[0] * d[0] + d[1] * d[1];
199+
if l2 > m {
200+
m = l2;
201+
ans = d[0] * d[1];
202+
} else if l2 == m {
203+
ans = ans.max(d[0] * d[1]);
204+
}
205+
}
206+
ans
207+
}
208+
}
209+
```
210+
211+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/150871371)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/08/26/LeetCode%203000.%E5%AF%B9%E8%A7%92%E7%BA%BF%E6%9C%80%E9%95%BF%E7%9A%84%E7%9F%A9%E5%BD%A2%E7%9A%84%E9%9D%A2%E7%A7%AF/)~
212+
>
213+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

0 commit comments

Comments
 (0)