Skip to content

Commit 33d81bb

Browse files
authored
Merge pull request #1296 from 0xff-dev/3000
Add solution and test-cases for problem 3000
2 parents 9c35691 + c2f8df3 commit 33d81bb

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

leetcode/2901-3000/3000.Maximum-Area-of-Longest-Diagonal-Rectangle/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [3000.Maximum Area of Longest Diagonal Rectangle][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a 2D **0-indexed** integer array `dimensions`.
5+
6+
For all indices `i`, `0 <= i < dimensions.length`, `dimensions[i][0]` represents the length and `dimensions[i][1]` represents the width of the rectangle `i`.
7+
8+
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.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: dimensions = [[9,3],[8,6]]
14+
Output: 48
15+
Explanation:
16+
For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487.
17+
For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10.
18+
So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48.
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Maximum Area of Longest Diagonal Rectangle
23-
```go
2423
```
25-
24+
Input: dimensions = [[3,4],[4,3]]
25+
Output: 12
26+
Explanation: Length of diagonal is the same for both which is 5, so maximum area = 12.
27+
```
2628

2729
## 结语
2830

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(dimensions [][]int) int {
4+
diagonal, area := 0, 0
5+
for _, n := range dimensions {
6+
tmp := n[0]*n[0] + n[1]*n[1]
7+
if tmp == diagonal {
8+
area = max(area, n[0]*n[1])
9+
}
10+
if tmp > diagonal {
11+
diagonal = tmp
12+
area = n[0] * n[1]
13+
}
14+
}
15+
16+
return area
517
}

leetcode/2901-3000/3000.Maximum-Area-of-Longest-Diagonal-Rectangle/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{9, 3}, {8, 6}}, 48},
17+
{"TestCase2", [][]int{{3, 4}, {4, 3}}, 12},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)