Skip to content

Commit e71c759

Browse files
authored
Merge pull request #1292 from 0xff-dev/3195
Add solution and test-cases for problem 3195
2 parents 4aefdae + 205ae9b commit e71c759

File tree

5 files changed

+45
-22
lines changed

5 files changed

+45
-22
lines changed
12.2 KB
Loading
7.37 KB
Loading

leetcode/3101-3200/3195.Find-the-Minimum-Area-to-Cover-All-Ones-I/README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [3195.Find the Minimum Area to Cover All Ones I][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 **binary** array `grid`. Find a rectangle with horizontal and vertical sides with the **smallest** area, such that all the 1's in `grid` lie inside this rectangle.
5+
6+
Return the **minimum** possible area of the rectangle.
7+
8+
**Example 1:**
79

8-
**Example 1:**
10+
![1](./1.png)
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: grid = [[0,1,0],[1,0,1]]
14+
15+
Output: 6
16+
17+
Explanation:
18+
19+
The smallest rectangle has a height of 2 and a width of 3, so it has an area of 2 * 3 = 6.
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
1723

18-
## 题解
24+
![2](./2.png)
1925

20-
### 思路1
21-
> ...
22-
Find the Minimum Area to Cover All Ones I
23-
```go
2426
```
27+
Input: grid = [[1,0],[0,0]]
28+
29+
Output: 1
2530
31+
Explanation:
32+
33+
The smallest rectangle has both height and width 1, so its area is 1 * 1 = 1.
34+
```
2635

2736
## 结语
2837

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(grid [][]int) int {
4+
leftTop, rightBottom := [2]int{1001, 1001}, [2]int{-1, -1}
5+
rows, cols := len(grid), len(grid[0])
6+
for i := 0; i < rows; i++ {
7+
for j := 0; j < cols; j++ {
8+
if grid[i][j] == 1 {
9+
leftTop[0] = min(leftTop[0], i)
10+
leftTop[1] = min(leftTop[1], j)
11+
12+
rightBottom[0] = max(rightBottom[0], i)
13+
rightBottom[1] = max(rightBottom[1], j)
14+
}
15+
}
16+
}
17+
x := rightBottom[0] - leftTop[0]
18+
y := rightBottom[1] - leftTop[1]
19+
return (x + 1) * (y + 1)
520
}

leetcode/3101-3200/3195.Find-the-Minimum-Area-to-Cover-All-Ones-I/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{{0, 1, 0}, {1, 0, 1}}, 6},
17+
{"TestCase2", [][]int{{1, 0}, {0, 0}}, 1},
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)