Skip to content

Commit 54f7548

Browse files
authored
Merge pull request #1293 from 0xff-dev/3197
Add solution and test-cases for problem 3197
2 parents f38bff5 + df3edfc commit 54f7548

File tree

5 files changed

+95
-22
lines changed

5 files changed

+95
-22
lines changed
7.45 KB
Loading
9.19 KB
Loading

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
# [3197.Find the Minimum Area to Cover All Ones II][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`. You need to find 3 **non-overlapping** rectangles having **non-zero** areas with horizontal and vertical sides such that all the 1's in `grid` lie inside these rectangles.
5+
6+
Return the **minimum** possible sum of the area of these rectangles.
7+
8+
**Note** that the rectangles are allowed to touch.
79

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

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: grid = [[1,0,1],[1,1,1]]
16+
17+
Output: 5
18+
19+
Explanation:
20+
21+
The 1's at (0, 0) and (1, 0) are covered by a rectangle of area 2.
22+
The 1's at (0, 2) and (1, 2) are covered by a rectangle of area 2.
23+
The 1 at (1, 1) is covered by a rectangle of area 1.
1324
```
1425

15-
## 题意
16-
> ...
26+
**Example 2:**
1727

18-
## 题解
28+
![2](./2.png)
1929

20-
### 思路1
21-
> ...
22-
Find the Minimum Area to Cover All Ones II
23-
```go
2430
```
31+
Input: grid = [[1,0,1,0],[0,1,0,1]]
32+
33+
Output: 5
2534
35+
Explanation:
36+
37+
The 1's at (0, 0) and (0, 2) are covered by a rectangle of area 3.
38+
The 1 at (1, 1) is covered by a rectangle of area 1.
39+
The 1 at (1, 3) is covered by a rectangle of area 1.
40+
```
2641

2742
## 结语
2843

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "math"
4+
5+
func minimumSum2(grid [][]int, u, d, l, r int) int {
6+
min_i, max_i := len(grid), 0
7+
min_j, max_j := len(grid[0]), 0
8+
for i := u; i <= d; i++ {
9+
for j := l; j <= r; j++ {
10+
if grid[i][j] == 1 {
11+
min_i = min(min_i, i)
12+
min_j = min(min_j, j)
13+
max_i = max(max_i, i)
14+
max_j = max(max_j, j)
15+
}
16+
}
17+
}
18+
if min_i <= max_i {
19+
return (max_i - min_i + 1) * (max_j - min_j + 1)
20+
}
21+
return math.MaxInt32 / 3
22+
}
23+
24+
func rotate(vec [][]int) [][]int {
25+
n, m := len(vec), len(vec[0])
26+
ret := make([][]int, m)
27+
for i := range ret {
28+
ret[i] = make([]int, n)
29+
}
30+
for i := 0; i < n; i++ {
31+
for j := 0; j < m; j++ {
32+
ret[m-j-1][i] = vec[i][j]
33+
}
34+
}
35+
return ret
36+
}
37+
38+
func solve(grid [][]int) int {
39+
n, m := len(grid), len(grid[0])
40+
res := n * m
41+
for i := 0; i+1 < n; i++ {
42+
for j := 0; j+1 < m; j++ {
43+
res = min(res, minimumSum2(grid, 0, i, 0, m-1)+
44+
minimumSum2(grid, i+1, n-1, 0, j)+
45+
minimumSum2(grid, i+1, n-1, j+1, m-1))
46+
res = min(res, minimumSum2(grid, 0, i, 0, j)+
47+
minimumSum2(grid, 0, i, j+1, m-1)+
48+
minimumSum2(grid, i+1, n-1, 0, m-1))
49+
}
50+
}
51+
for i := 0; i+2 < n; i++ {
52+
for j := i + 1; j+1 < n; j++ {
53+
res = min(res, minimumSum2(grid, 0, i, 0, m-1)+
54+
minimumSum2(grid, i+1, j, 0, m-1)+
55+
minimumSum2(grid, j+1, n-1, 0, m-1))
56+
}
57+
}
58+
return res
59+
}
60+
61+
func Solution(grid [][]int) int {
62+
rgrid := rotate(grid)
63+
return min(solve(grid), solve(rgrid))
564
}

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