From 255147e912ff9661d87335a9453c82eaefbc29a7 Mon Sep 17 00:00:00 2001 From: rain84 Date: Sun, 27 Oct 2024 22:35:38 +0300 Subject: [PATCH] feat: add solutions to lc problem: No.1277 --- .../README.md | 52 ++++++++++++++++- .../README_EN.md | 56 +++++++++++++++++-- .../Solution.js | 18 ++++++ .../Solution.ts | 18 ++++++ 4 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 solution/1200-1299/1277.Count Square Submatrices with All Ones/Solution.js create mode 100644 solution/1200-1299/1277.Count Square Submatrices with All Ones/Solution.ts diff --git a/solution/1200-1299/1277.Count Square Submatrices with All Ones/README.md b/solution/1200-1299/1277.Count Square Submatrices with All Ones/README.md index 9deb5f3c5ecf3..9164b3dfc0177 100644 --- a/solution/1200-1299/1277.Count Square Submatrices with All Ones/README.md +++ b/solution/1200-1299/1277.Count Square Submatrices with All Ones/README.md @@ -33,7 +33,7 @@ tags:   [0,1,1,1] ] 输出:15 -解释: +解释: 边长为 1 的正方形有 10 个。 边长为 2 的正方形有 4 个。 边长为 3 的正方形有 1 个。 @@ -42,7 +42,7 @@ tags:

示例 2:

-
输入:matrix = 
+
输入:matrix =
 [
   [1,0,1],
   [1,1,0],
@@ -50,7 +50,7 @@ tags:
 ]
 输出:7
 解释:
-边长为 1 的正方形有 6 个。 
+边长为 1 的正方形有 6 个。
 边长为 2 的正方形有 1 个。
 正方形的总数 = 6 + 1 = 7.
 
@@ -172,6 +172,52 @@ func countSquares(matrix [][]int) int { } ``` +#### TypeScript + +```ts +function countSquares(matrix: number[][]): number { + const [m, n] = [matrix.length, matrix[0].length]; + const f = Array.from({ length: m }, () => Array(n)); + const dfs = (i: number, j: number): number => { + if (i === m || j === n || !matrix[i][j]) return 0; + f[i][j] ??= 1 + Math.min(dfs(i + 1, j), dfs(i, j + 1), dfs(i + 1, j + 1)); + return f[i][j]; + }; + let ans = 0; + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + ans += dfs(i, j); + } + } + + return ans; +} +``` + +#### JavaScript + +```js +function countSquares(matrix) { + const [m, n] = [matrix.length, matrix[0].length]; + const f = Array.from({ length: m }, () => Array(n)); + const dfs = (i, j) => { + if (i === m || j === n || !matrix[i][j]) return 0; + f[i][j] ??= 1 + Math.min(dfs(i + 1, j), dfs(i, j + 1), dfs(i + 1, j + 1)); + return f[i][j]; + }; + let ans = 0; + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + ans += dfs(i, j); + } + } + + return ans; +} +``` + diff --git a/solution/1200-1299/1277.Count Square Submatrices with All Ones/README_EN.md b/solution/1200-1299/1277.Count Square Submatrices with All Ones/README_EN.md index c706d0ad605f1..385e2eba4cbf8 100644 --- a/solution/1200-1299/1277.Count Square Submatrices with All Ones/README_EN.md +++ b/solution/1200-1299/1277.Count Square Submatrices with All Ones/README_EN.md @@ -33,7 +33,7 @@ tags:   [0,1,1,1] ] Output: 15 -Explanation: +Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. @@ -43,16 +43,16 @@ Total number of squares = 10 + 4 + 1 = 15.

Example 2:

-Input: matrix = 
+Input: matrix =
 [
   [1,0,1],
   [1,1,0],
   [1,1,0]
 ]
 Output: 7
-Explanation: 
-There are 6 squares of side 1.  
-There is 1 square of side 2. 
+Explanation:
+There are 6 squares of side 1.
+There is 1 square of side 2.
 Total number of squares = 6 + 1 = 7.
 
@@ -172,6 +172,52 @@ func countSquares(matrix [][]int) int { } ``` +#### TypeScript + +```ts +function countSquares(matrix: number[][]): number { + const [m, n] = [matrix.length, matrix[0].length]; + const f = Array.from({ length: m }, () => Array(n)); + const dfs = (i: number, j: number): number => { + if (i === m || j === n || !matrix[i][j]) return 0; + f[i][j] ??= 1 + Math.min(dfs(i + 1, j), dfs(i, j + 1), dfs(i + 1, j + 1)); + return f[i][j]; + }; + let ans = 0; + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + ans += dfs(i, j); + } + } + + return ans; +} +``` + +#### JavaScript + +```js +function countSquares(matrix) { + const [m, n] = [matrix.length, matrix[0].length]; + const f = Array.from({ length: m }, () => Array(n)); + const dfs = (i, j) => { + if (i === m || j === n || !matrix[i][j]) return 0; + f[i][j] ??= 1 + Math.min(dfs(i + 1, j), dfs(i, j + 1), dfs(i + 1, j + 1)); + return f[i][j]; + }; + let ans = 0; + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + ans += dfs(i, j); + } + } + + return ans; +} +``` + diff --git a/solution/1200-1299/1277.Count Square Submatrices with All Ones/Solution.js b/solution/1200-1299/1277.Count Square Submatrices with All Ones/Solution.js new file mode 100644 index 0000000000000..422c84c168eff --- /dev/null +++ b/solution/1200-1299/1277.Count Square Submatrices with All Ones/Solution.js @@ -0,0 +1,18 @@ +function countSquares(matrix) { + const [m, n] = [matrix.length, matrix[0].length]; + const f = Array.from({ length: m }, () => Array(n)); + const dfs = (i, j) => { + if (i === m || j === n || !matrix[i][j]) return 0; + f[i][j] ??= 1 + Math.min(dfs(i + 1, j), dfs(i, j + 1), dfs(i + 1, j + 1)); + return f[i][j]; + }; + let ans = 0; + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + ans += dfs(i, j); + } + } + + return ans; +} diff --git a/solution/1200-1299/1277.Count Square Submatrices with All Ones/Solution.ts b/solution/1200-1299/1277.Count Square Submatrices with All Ones/Solution.ts new file mode 100644 index 0000000000000..15f02bbe04c42 --- /dev/null +++ b/solution/1200-1299/1277.Count Square Submatrices with All Ones/Solution.ts @@ -0,0 +1,18 @@ +function countSquares(matrix: number[][]): number { + const [m, n] = [matrix.length, matrix[0].length]; + const f = Array.from({ length: m }, () => Array(n)); + const dfs = (i: number, j: number): number => { + if (i === m || j === n || !matrix[i][j]) return 0; + f[i][j] ??= 1 + Math.min(dfs(i + 1, j), dfs(i, j + 1), dfs(i + 1, j + 1)); + return f[i][j]; + }; + let ans = 0; + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + ans += dfs(i, j); + } + } + + return ans; +}