Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 0ec16f2

Browse files
committed
1074 accepted. 2700 ms, faster than 37.93%
1 parent 9a0531a commit 0ec16f2

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
package problem1074
22

3-
func numSubmatrixSumTarget(matrix [][]int, target int) int {
3+
func numSubmatrixSumTarget(M [][]int, target int) int {
4+
m, n := len(M), len(M[0])
45

5-
return 0
6+
sums := [301][301]int{}
7+
for i := 1; i <= m; i++ {
8+
t := 0
9+
for j := 1; j <= n; j++ {
10+
t += M[i-1][j-1]
11+
sums[i][j] = t + sums[i-1][j]
12+
}
13+
}
14+
15+
res := 0
16+
17+
for x1 := 0; x1 < m; x1++ {
18+
for x2 := x1 + 1; x2 <= m; x2++ {
19+
for y1 := 0; y1 < n; y1++ {
20+
for y2 := y1 + 1; y2 <= n; y2++ {
21+
s := sums[x2][y2] - sums[x1][y2] - sums[x2][y1] + sums[x1][y1]
22+
if s == target {
23+
res++
24+
}
25+
}
26+
}
27+
}
28+
}
29+
30+
return res
631
}

Algorithms/1074.number-of-submatrices-that-sum-to-target/number-of-submatrices-that-sum-to-target_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ var tcs = []struct {
1414
}{
1515

1616
{
17-
[][]int{{0, 1, 0}, {1, 1, 1}, {0, 1, 0}},
17+
[][]int{{1, -1}, {-1, 1}},
1818
0,
19-
4,
19+
5,
2020
},
2121

2222
{
23-
[][]int{{1, -1}, {-1, 1}},
23+
[][]int{{0, 1, 0}, {1, 1, 1}, {0, 1, 0}},
2424
0,
25-
5,
25+
4,
2626
},
2727

2828
// 可以有多个 testcase

0 commit comments

Comments
 (0)