Skip to content

Commit e04255e

Browse files
authored
Merge pull request #1291 from 0xff-dev/1504
Add solution and test-cases for problem 1504
2 parents e71c759 + 36f2718 commit e04255e

File tree

5 files changed

+58
-23
lines changed

5 files changed

+58
-23
lines changed
6.45 KB
Loading
8.14 KB
Loading

leetcode/1501-1600/1504.Count-Submatrices-With-All-Ones/README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [1504.Count Submatrices With All Ones][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+
Given an `m x n` binary matrix `mat`, return the number of **submatrices** that have all ones.
5+
6+
**Example 1:**
77

8-
**Example 1:**
8+
![1](./1.jpg)
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: mat = [[1,0,1],[1,1,0],[1,1,0]]
12+
Output: 13
13+
Explanation:
14+
There are 6 rectangles of side 1x1.
15+
There are 2 rectangles of side 1x2.
16+
There are 3 rectangles of side 2x1.
17+
There is 1 rectangle of side 2x2.
18+
There is 1 rectangle of side 3x1.
19+
Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
1320
```
1421

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

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

20-
### 思路1
21-
> ...
22-
Count Submatrices With All Ones
23-
```go
2426
```
25-
27+
Input: mat = [[0,1,1,0],[0,1,1,1],[1,1,1,0]]
28+
Output: 24
29+
Explanation:
30+
There are 8 rectangles of side 1x1.
31+
There are 5 rectangles of side 1x2.
32+
There are 2 rectangles of side 1x3.
33+
There are 4 rectangles of side 2x1.
34+
There are 2 rectangles of side 2x2.
35+
There are 2 rectangles of side 3x1.
36+
There is 1 rectangle of side 3x2.
37+
Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24.
38+
```
2639

2740
## 结语
2841

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(mat [][]int) int {
4+
n := len(mat[0])
5+
heights := make([]int, n)
6+
res := 0
7+
for _, row := range mat {
8+
for i := 0; i < n; i++ {
9+
if row[i] == 0 {
10+
heights[i] = 0
11+
} else {
12+
heights[i]++
13+
}
14+
}
15+
stack := [][3]int{{-1, 0, -1}}
16+
for i, h := range heights {
17+
for len(stack) > 1 && stack[len(stack)-1][2] >= h {
18+
stack = stack[:len(stack)-1]
19+
}
20+
top := stack[len(stack)-1]
21+
j, prev := top[0], top[1]
22+
cur := prev + (i-j)*h
23+
stack = append(stack, [3]int{i, cur, h})
24+
res += cur
25+
}
26+
}
27+
return res
528
}

leetcode/1501-1600/1504.Count-Submatrices-With-All-Ones/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, 0}, {1, 1, 0}}, 13},
17+
{"TestCase2", [][]int{{0, 1, 1, 0}, {0, 1, 1, 1}, {1, 1, 1, 0}}, 24},
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)