Skip to content

Commit 047f721

Browse files
committed
feat: add solutions to lc problem: No.1504
No.1504.Count Submatrices With All Ones
1 parent 95114cb commit 047f721

File tree

4 files changed

+178
-7
lines changed

4 files changed

+178
-7
lines changed

solution/1500-1599/1504.Count Submatrices With All Ones/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,41 @@ function numSubmat(mat: number[][]): number {
228228
}
229229
```
230230

231+
#### JavaScript
232+
233+
```js
234+
/**
235+
* @param {number[][]} mat
236+
* @return {number}
237+
*/
238+
var numSubmat = function (mat) {
239+
const m = mat.length;
240+
const n = mat[0].length;
241+
const g = Array.from({ length: m }, () => Array(n).fill(0));
242+
243+
for (let i = 0; i < m; i++) {
244+
for (let j = 0; j < n; j++) {
245+
if (mat[i][j]) {
246+
g[i][j] = j === 0 ? 1 : 1 + g[i][j - 1];
247+
}
248+
}
249+
}
250+
251+
let ans = 0;
252+
for (let i = 0; i < m; i++) {
253+
for (let j = 0; j < n; j++) {
254+
let col = Infinity;
255+
for (let k = i; k >= 0; k--) {
256+
col = Math.min(col, g[k][j]);
257+
ans += col;
258+
}
259+
}
260+
}
261+
262+
return ans;
263+
};
264+
```
265+
231266
<!-- tabs:end -->
232267

233268
<!-- solution:end -->

solution/1500-1599/1504.Count Submatrices With All Ones/README_EN.md

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ tags:
3030
<pre>
3131
<strong>Input:</strong> mat = [[1,0,1],[1,1,0],[1,1,0]]
3232
<strong>Output:</strong> 13
33-
<strong>Explanation:</strong>
33+
<strong>Explanation:</strong>
3434
There are 6 rectangles of side 1x1.
3535
There are 2 rectangles of side 1x2.
3636
There are 3 rectangles of side 2x1.
37-
There is 1 rectangle of side 2x2.
37+
There is 1 rectangle of side 2x2.
3838
There is 1 rectangle of side 3x1.
3939
Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
4040
</pre>
@@ -44,14 +44,14 @@ Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
4444
<pre>
4545
<strong>Input:</strong> mat = [[0,1,1,0],[0,1,1,1],[1,1,1,0]]
4646
<strong>Output:</strong> 24
47-
<strong>Explanation:</strong>
47+
<strong>Explanation:</strong>
4848
There are 8 rectangles of side 1x1.
4949
There are 5 rectangles of side 1x2.
50-
There are 2 rectangles of side 1x3.
50+
There are 2 rectangles of side 1x3.
5151
There are 4 rectangles of side 2x1.
52-
There are 2 rectangles of side 2x2.
53-
There are 2 rectangles of side 3x1.
54-
There is 1 rectangle of side 3x2.
52+
There are 2 rectangles of side 2x2.
53+
There are 2 rectangles of side 3x1.
54+
There is 1 rectangle of side 3x2.
5555
Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24.
5656
</pre>
5757

@@ -221,6 +221,79 @@ function numSubmat(mat: number[][]): number {
221221
}
222222
```
223223

224+
#### Rust
225+
226+
```rust
227+
impl Solution {
228+
pub fn num_submat(mat: Vec<Vec<i32>>) -> i32 {
229+
let m = mat.len();
230+
let n = mat[0].len();
231+
let mut g = vec![vec![0; n]; m];
232+
233+
for i in 0..m {
234+
for j in 0..n {
235+
if mat[i][j] == 1 {
236+
if j == 0 {
237+
g[i][j] = 1;
238+
} else {
239+
g[i][j] = 1 + g[i][j - 1];
240+
}
241+
}
242+
}
243+
}
244+
245+
let mut ans = 0;
246+
for i in 0..m {
247+
for j in 0..n {
248+
let mut col = i32::MAX;
249+
let mut k = i as i32;
250+
while k >= 0 && col > 0 {
251+
col = col.min(g[k as usize][j]);
252+
ans += col;
253+
k -= 1;
254+
}
255+
}
256+
}
257+
ans
258+
}
259+
}
260+
```
261+
262+
#### JavaScript
263+
264+
```js
265+
/**
266+
* @param {number[][]} mat
267+
* @return {number}
268+
*/
269+
var numSubmat = function (mat) {
270+
const m = mat.length;
271+
const n = mat[0].length;
272+
const g = Array.from({ length: m }, () => Array(n).fill(0));
273+
274+
for (let i = 0; i < m; i++) {
275+
for (let j = 0; j < n; j++) {
276+
if (mat[i][j]) {
277+
g[i][j] = j === 0 ? 1 : 1 + g[i][j - 1];
278+
}
279+
}
280+
}
281+
282+
let ans = 0;
283+
for (let i = 0; i < m; i++) {
284+
for (let j = 0; j < n; j++) {
285+
let col = Infinity;
286+
for (let k = i; k >= 0; k--) {
287+
col = Math.min(col, g[k][j]);
288+
ans += col;
289+
}
290+
}
291+
}
292+
293+
return ans;
294+
};
295+
```
296+
224297
<!-- tabs:end -->
225298

226299
<!-- solution:end -->
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[][]} mat
3+
* @return {number}
4+
*/
5+
var numSubmat = function (mat) {
6+
const m = mat.length;
7+
const n = mat[0].length;
8+
const g = Array.from({ length: m }, () => Array(n).fill(0));
9+
10+
for (let i = 0; i < m; i++) {
11+
for (let j = 0; j < n; j++) {
12+
if (mat[i][j]) {
13+
g[i][j] = j === 0 ? 1 : 1 + g[i][j - 1];
14+
}
15+
}
16+
}
17+
18+
let ans = 0;
19+
for (let i = 0; i < m; i++) {
20+
for (let j = 0; j < n; j++) {
21+
let col = Infinity;
22+
for (let k = i; k >= 0; k--) {
23+
col = Math.min(col, g[k][j]);
24+
ans += col;
25+
}
26+
}
27+
}
28+
29+
return ans;
30+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
impl Solution {
2+
pub fn num_submat(mat: Vec<Vec<i32>>) -> i32 {
3+
let m = mat.len();
4+
let n = mat[0].len();
5+
let mut g = vec![vec![0; n]; m];
6+
7+
for i in 0..m {
8+
for j in 0..n {
9+
if mat[i][j] == 1 {
10+
if j == 0 {
11+
g[i][j] = 1;
12+
} else {
13+
g[i][j] = 1 + g[i][j - 1];
14+
}
15+
}
16+
}
17+
}
18+
19+
let mut ans = 0;
20+
for i in 0..m {
21+
for j in 0..n {
22+
let mut col = i32::MAX;
23+
let mut k = i as i32;
24+
while k >= 0 && col > 0 {
25+
col = col.min(g[k as usize][j]);
26+
ans += col;
27+
k -= 1;
28+
}
29+
}
30+
}
31+
ans
32+
}
33+
}

0 commit comments

Comments
 (0)