diff --git a/solution/1500-1599/1504.Count Submatrices With All Ones/README.md b/solution/1500-1599/1504.Count Submatrices With All Ones/README.md index 0c9288e87a604..0b1d65f4ca039 100644 --- a/solution/1500-1599/1504.Count Submatrices With All Ones/README.md +++ b/solution/1500-1599/1504.Count Submatrices With All Ones/README.md @@ -228,6 +228,79 @@ function numSubmat(mat: number[][]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn num_submat(mat: Vec>) -> i32 { + let m = mat.len(); + let n = mat[0].len(); + let mut g = vec![vec![0; n]; m]; + + for i in 0..m { + for j in 0..n { + if mat[i][j] == 1 { + if j == 0 { + g[i][j] = 1; + } else { + g[i][j] = 1 + g[i][j - 1]; + } + } + } + } + + let mut ans = 0; + for i in 0..m { + for j in 0..n { + let mut col = i32::MAX; + let mut k = i as i32; + while k >= 0 && col > 0 { + col = col.min(g[k as usize][j]); + ans += col; + k -= 1; + } + } + } + ans + } +} +``` + +#### JavaScript + +```js +/** + * @param {number[][]} mat + * @return {number} + */ +var numSubmat = function (mat) { + const m = mat.length; + const n = mat[0].length; + const g = Array.from({ length: m }, () => Array(n).fill(0)); + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (mat[i][j]) { + g[i][j] = j === 0 ? 1 : 1 + g[i][j - 1]; + } + } + } + + let ans = 0; + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + let col = Infinity; + for (let k = i; k >= 0; k--) { + col = Math.min(col, g[k][j]); + ans += col; + } + } + } + + return ans; +}; +``` + diff --git a/solution/1500-1599/1504.Count Submatrices With All Ones/README_EN.md b/solution/1500-1599/1504.Count Submatrices With All Ones/README_EN.md index b872397a9ea3b..b0d87137922b9 100644 --- a/solution/1500-1599/1504.Count Submatrices With All Ones/README_EN.md +++ b/solution/1500-1599/1504.Count Submatrices With All Ones/README_EN.md @@ -30,11 +30,11 @@ tags:
 Input: mat = [[1,0,1],[1,1,0],[1,1,0]]
 Output: 13
-Explanation: 
+Explanation:
 There are 6 rectangles of side 1x1.
 There are 2 rectangles of side 1x2.
 There are 3 rectangles of side 2x1.
-There is 1 rectangle of side 2x2. 
+There is 1 rectangle of side 2x2.
 There is 1 rectangle of side 3x1.
 Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
 
@@ -44,14 +44,14 @@ Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
 Input: mat = [[0,1,1,0],[0,1,1,1],[1,1,1,0]]
 Output: 24
-Explanation: 
+Explanation:
 There are 8 rectangles of side 1x1.
 There are 5 rectangles of side 1x2.
-There are 2 rectangles of side 1x3. 
+There are 2 rectangles of side 1x3.
 There are 4 rectangles of side 2x1.
-There are 2 rectangles of side 2x2. 
-There are 2 rectangles of side 3x1. 
-There is 1 rectangle of side 3x2. 
+There are 2 rectangles of side 2x2.
+There are 2 rectangles of side 3x1.
+There is 1 rectangle of side 3x2.
 Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24.
 
@@ -221,6 +221,79 @@ function numSubmat(mat: number[][]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn num_submat(mat: Vec>) -> i32 { + let m = mat.len(); + let n = mat[0].len(); + let mut g = vec![vec![0; n]; m]; + + for i in 0..m { + for j in 0..n { + if mat[i][j] == 1 { + if j == 0 { + g[i][j] = 1; + } else { + g[i][j] = 1 + g[i][j - 1]; + } + } + } + } + + let mut ans = 0; + for i in 0..m { + for j in 0..n { + let mut col = i32::MAX; + let mut k = i as i32; + while k >= 0 && col > 0 { + col = col.min(g[k as usize][j]); + ans += col; + k -= 1; + } + } + } + ans + } +} +``` + +#### JavaScript + +```js +/** + * @param {number[][]} mat + * @return {number} + */ +var numSubmat = function (mat) { + const m = mat.length; + const n = mat[0].length; + const g = Array.from({ length: m }, () => Array(n).fill(0)); + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (mat[i][j]) { + g[i][j] = j === 0 ? 1 : 1 + g[i][j - 1]; + } + } + } + + let ans = 0; + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + let col = Infinity; + for (let k = i; k >= 0; k--) { + col = Math.min(col, g[k][j]); + ans += col; + } + } + } + + return ans; +}; +``` + diff --git a/solution/1500-1599/1504.Count Submatrices With All Ones/Solution.js b/solution/1500-1599/1504.Count Submatrices With All Ones/Solution.js new file mode 100644 index 0000000000000..4c3c3253bd285 --- /dev/null +++ b/solution/1500-1599/1504.Count Submatrices With All Ones/Solution.js @@ -0,0 +1,30 @@ +/** + * @param {number[][]} mat + * @return {number} + */ +var numSubmat = function (mat) { + const m = mat.length; + const n = mat[0].length; + const g = Array.from({ length: m }, () => Array(n).fill(0)); + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (mat[i][j]) { + g[i][j] = j === 0 ? 1 : 1 + g[i][j - 1]; + } + } + } + + let ans = 0; + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + let col = Infinity; + for (let k = i; k >= 0; k--) { + col = Math.min(col, g[k][j]); + ans += col; + } + } + } + + return ans; +}; diff --git a/solution/1500-1599/1504.Count Submatrices With All Ones/Solution.rs b/solution/1500-1599/1504.Count Submatrices With All Ones/Solution.rs new file mode 100644 index 0000000000000..5312023847cad --- /dev/null +++ b/solution/1500-1599/1504.Count Submatrices With All Ones/Solution.rs @@ -0,0 +1,33 @@ +impl Solution { + pub fn num_submat(mat: Vec>) -> i32 { + let m = mat.len(); + let n = mat[0].len(); + let mut g = vec![vec![0; n]; m]; + + for i in 0..m { + for j in 0..n { + if mat[i][j] == 1 { + if j == 0 { + g[i][j] = 1; + } else { + g[i][j] = 1 + g[i][j - 1]; + } + } + } + } + + let mut ans = 0; + for i in 0..m { + for j in 0..n { + let mut col = i32::MAX; + let mut k = i as i32; + while k >= 0 && col > 0 { + col = col.min(g[k as usize][j]); + ans += col; + k -= 1; + } + } + } + ans + } +}