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

Commit 9a0531a

Browse files
committed
1074 added
1 parent b8dff93 commit 9a0531a

File tree

5 files changed

+99
-16
lines changed

5 files changed

+99
-16
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@
154154
"strings",
155155
"strs",
156156
"subarray",
157+
"submatrices",
158+
"submatrix",
157159
"subtrees",
158160
"superpalindrome",
159161
"superpalindromes",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [1074. Number of Submatrices That Sum to Target](https://leetcode.com/problems/number-of-submatrices-that-sum-to-target/)
2+
3+
Given a matrix, and a target, return the number of non-empty submatrices that sum to target.
4+
5+
A submatrix x1, y1, x2, y2 is the set of all cells matrix[x][y] with x1 <= x <= x2 and y1 <= y <= y2.
6+
7+
Two submatrices (x1, y1, x2, y2) and (x1', y1', x2', y2') are different if they have some coordinate that is different: for example, if x1 != x1'.
8+
9+
Example 1:
10+
11+
```text
12+
Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
13+
Output: 4
14+
Explanation: The four 1x1 submatrices that only contain 0.
15+
```
16+
17+
Example 2:
18+
19+
```text
20+
Input: matrix = [[1,-1],[-1,1]], target = 0
21+
Output: 5
22+
Explanation: The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.
23+
```
24+
25+
Note:
26+
27+
1. `1 <= matrix.length <= 300`
28+
1. `1 <= matrix[0].length <= 300`
29+
1. `-1000 <= matrix[i] <= 1000`
30+
1. `-10^8 <= target <= 10^8`
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package problem1074
2+
3+
func numSubmatrixSumTarget(matrix [][]int, target int) int {
4+
5+
return 0
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package problem1074
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// tcs is testcase slice
10+
var tcs = []struct {
11+
matrix [][]int
12+
target int
13+
ans int
14+
}{
15+
16+
{
17+
[][]int{{0, 1, 0}, {1, 1, 1}, {0, 1, 0}},
18+
0,
19+
4,
20+
},
21+
22+
{
23+
[][]int{{1, -1}, {-1, 1}},
24+
0,
25+
5,
26+
},
27+
28+
// 可以有多个 testcase
29+
}
30+
31+
func Test_numSubmatrixSumTarget(t *testing.T) {
32+
ast := assert.New(t)
33+
34+
for _, tc := range tcs {
35+
ast.Equal(tc.ans, numSubmatrixSumTarget(tc.matrix, tc.target), "输入:%v", tc)
36+
}
37+
}
38+
39+
func Benchmark_numSubmatrixSumTarget(b *testing.B) {
40+
for i := 0; i < b.N; i++ {
41+
for _, tc := range tcs {
42+
numSubmatrixSumTarget(tc.matrix, tc.target)
43+
}
44+
}
45+
}

leetcode.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
33
"Ranking": 588,
4-
"Updated": "2019-07-12T20:42:24.791388303+08:00",
4+
"Updated": "2019-07-13T21:08:01.507144741+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 250,
@@ -325,7 +325,7 @@
325325
"ID": 25,
326326
"Title": "Reverse Nodes in k-Group",
327327
"TitleSlug": "reverse-nodes-in-k-group",
328-
"PassRate": "36%",
328+
"PassRate": "37%",
329329
"Difficulty": "Hard",
330330
"IsAccepted": true,
331331
"IsPaid": false,
@@ -4525,7 +4525,7 @@
45254525
"ID": 375,
45264526
"Title": "Guess Number Higher or Lower II",
45274527
"TitleSlug": "guess-number-higher-or-lower-ii",
4528-
"PassRate": "37%",
4528+
"PassRate": "38%",
45294529
"Difficulty": "Medium",
45304530
"IsAccepted": true,
45314531
"IsPaid": false,
@@ -4645,7 +4645,7 @@
46454645
"ID": 385,
46464646
"Title": "Mini Parser",
46474647
"TitleSlug": "mini-parser",
4648-
"PassRate": "31%",
4648+
"PassRate": "32%",
46494649
"Difficulty": "Medium",
46504650
"IsAccepted": true,
46514651
"IsPaid": false,
@@ -8581,7 +8581,7 @@
85818581
"ID": 713,
85828582
"Title": "Subarray Product Less Than K",
85838583
"TitleSlug": "subarray-product-less-than-k",
8584-
"PassRate": "36%",
8584+
"PassRate": "37%",
85858585
"Difficulty": "Medium",
85868586
"IsAccepted": true,
85878587
"IsPaid": false,
@@ -9913,7 +9913,7 @@
99139913
"ID": 824,
99149914
"Title": "Goat Latin",
99159915
"TitleSlug": "goat-latin",
9916-
"PassRate": "57%",
9916+
"PassRate": "58%",
99179917
"Difficulty": "Easy",
99189918
"IsAccepted": true,
99199919
"IsPaid": false,
@@ -10669,7 +10669,7 @@
1066910669
"ID": 887,
1067010670
"Title": "Super Egg Drop",
1067110671
"TitleSlug": "super-egg-drop",
10672-
"PassRate": "25%",
10672+
"PassRate": "24%",
1067310673
"Difficulty": "Hard",
1067410674
"IsAccepted": true,
1067510675
"IsPaid": false,
@@ -11797,7 +11797,7 @@
1179711797
"ID": 981,
1179811798
"Title": "Time Based Key-Value Store",
1179911799
"TitleSlug": "time-based-key-value-store",
11800-
"PassRate": "50%",
11800+
"PassRate": "51%",
1180111801
"Difficulty": "Medium",
1180211802
"IsAccepted": true,
1180311803
"IsPaid": false,
@@ -11929,7 +11929,7 @@
1192911929
"ID": 992,
1193011930
"Title": "Subarrays with K Different Integers",
1193111931
"TitleSlug": "subarrays-with-k-different-integers",
11932-
"PassRate": "45%",
11932+
"PassRate": "44%",
1193311933
"Difficulty": "Hard",
1193411934
"IsAccepted": true,
1193511935
"IsPaid": false,
@@ -12493,7 +12493,7 @@
1249312493
"ID": 1039,
1249412494
"Title": "Minimum Score Triangulation of Polygon",
1249512495
"TitleSlug": "minimum-score-triangulation-of-polygon",
12496-
"PassRate": "41%",
12496+
"PassRate": "42%",
1249712497
"Difficulty": "Medium",
1249812498
"IsAccepted": true,
1249912499
"IsPaid": false,
@@ -12997,7 +12997,7 @@
1299712997
"ID": 1081,
1299812998
"Title": "Smallest Subsequence of Distinct Characters",
1299912999
"TitleSlug": "smallest-subsequence-of-distinct-characters",
13000-
"PassRate": "42%",
13000+
"PassRate": "43%",
1300113001
"Difficulty": "Medium",
1300213002
"IsAccepted": false,
1300313003
"IsPaid": false,
@@ -13081,7 +13081,7 @@
1308113081
"ID": 1088,
1308213082
"Title": "Confusing Number II",
1308313083
"TitleSlug": "confusing-number-ii",
13084-
"PassRate": "35%",
13084+
"PassRate": "34%",
1308513085
"Difficulty": "Hard",
1308613086
"IsAccepted": false,
1308713087
"IsPaid": true,
@@ -13326,7 +13326,7 @@
1332613326
"IsAccepted": false,
1332713327
"IsPaid": false,
1332813328
"IsFavor": false,
13329-
"IsNew": true,
13329+
"IsNew": false,
1333013330
"HasNoGoOption": false
1333113331
},
1333213332
{
@@ -13338,7 +13338,7 @@
1333813338
"IsAccepted": false,
1333913339
"IsPaid": false,
1334013340
"IsFavor": false,
13341-
"IsNew": true,
13341+
"IsNew": false,
1334213342
"HasNoGoOption": false
1334313343
},
1334413344
{
@@ -13350,7 +13350,7 @@
1335013350
"IsAccepted": false,
1335113351
"IsPaid": false,
1335213352
"IsFavor": false,
13353-
"IsNew": true,
13353+
"IsNew": false,
1335413354
"HasNoGoOption": false
1335513355
},
1335613356
{
@@ -13362,7 +13362,7 @@
1336213362
"IsAccepted": false,
1336313363
"IsPaid": false,
1336413364
"IsFavor": false,
13365-
"IsNew": true,
13365+
"IsNew": false,
1336613366
"HasNoGoOption": false
1336713367
}
1336813368
]

0 commit comments

Comments
 (0)