From 0923a06749b5e0b6819106c0cee859c6e70dd5c4 Mon Sep 17 00:00:00 2001 From: rain84 Date: Fri, 9 Aug 2024 20:38:12 +0300 Subject: [PATCH 1/4] feat: add ts solution to lc problem: No.0840 --- .../0840.Magic Squares In Grid/README.md | 56 +++++++++++++++++++ .../0840.Magic Squares In Grid/README_EN.md | 56 +++++++++++++++++++ .../0840.Magic Squares In Grid/Solution2.ts | 41 ++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 solution/0800-0899/0840.Magic Squares In Grid/Solution2.ts diff --git a/solution/0800-0899/0840.Magic Squares In Grid/README.md b/solution/0800-0899/0840.Magic Squares In Grid/README.md index 338eb194fb833..a7479e429c702 100644 --- a/solution/0800-0899/0840.Magic Squares In Grid/README.md +++ b/solution/0800-0899/0840.Magic Squares In Grid/README.md @@ -322,4 +322,60 @@ function numMagicSquaresInside(grid: number[][]): number { + + +### Solution 2 + + + +#### TypeScript + +```ts +export function numMagicSquaresInside(grid: number[][]): number { + const [m, n] = [grid.length, grid[0].length] + if (m < 3 || n < 3) return 0 + + const check = (y: number, x: number) => { + const g = grid + if (g[y + 1][x + 1] !== 5) return 0 + + const cells = [ + g[y][x], + g[y][x + 1], + g[y][x + 2], + g[y + 1][x + 2], + g[y + 2][x + 2], + g[y + 2][x + 1], + g[y + 2][x], + g[y + 1][x], + ] + + const i = cells.indexOf(2) + if (i === -1) return 0 + cells.push(...cells.splice(0, i)) + + const circle = [2, 9, 4, 3, 8, 1, 6, 7] + const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9] + + if (cells.every((x, i) => x === circle[i])) return 1 + if (cells.every((x, i) => x === reverseCircle[i])) return 1 + + return 0 + } + + let res = 0 + for (let i = 0; i < m - 2; i++) { + for (let j = 0; j < n - 2; j++) { + res += check(i, j) + } + } + + return res +} +``` + + + + + diff --git a/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md b/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md index 5585d83cb0877..2d1b7ca524d09 100644 --- a/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md +++ b/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md @@ -318,4 +318,60 @@ function numMagicSquaresInside(grid: number[][]): number { + + +### Solution 2 + + + +#### TypeScript + +```ts +export function numMagicSquaresInside(grid: number[][]): number { + const [m, n] = [grid.length, grid[0].length] + if (m < 3 || n < 3) return 0 + + const check = (y: number, x: number) => { + const g = grid + if (g[y + 1][x + 1] !== 5) return 0 + + const cells = [ + g[y][x], + g[y][x + 1], + g[y][x + 2], + g[y + 1][x + 2], + g[y + 2][x + 2], + g[y + 2][x + 1], + g[y + 2][x], + g[y + 1][x], + ] + + const i = cells.indexOf(2) + if (i === -1) return 0 + cells.push(...cells.splice(0, i)) + + const circle = [2, 9, 4, 3, 8, 1, 6, 7] + const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9] + + if (cells.every((x, i) => x === circle[i])) return 1 + if (cells.every((x, i) => x === reverseCircle[i])) return 1 + + return 0 + } + + let res = 0 + for (let i = 0; i < m - 2; i++) { + for (let j = 0; j < n - 2; j++) { + res += check(i, j) + } + } + + return res +} +``` + + + + + diff --git a/solution/0800-0899/0840.Magic Squares In Grid/Solution2.ts b/solution/0800-0899/0840.Magic Squares In Grid/Solution2.ts new file mode 100644 index 0000000000000..2798cc4025b20 --- /dev/null +++ b/solution/0800-0899/0840.Magic Squares In Grid/Solution2.ts @@ -0,0 +1,41 @@ +function numMagicSquaresInside(grid: number[][]): number { + const [m, n] = [grid.length, grid[0].length]; + if (m < 3 || n < 3) return 0; + + const check = (y: number, x: number) => { + const g = grid; + if (g[y + 1][x + 1] !== 5) return 0; + + const cells = [ + g[y][x], + g[y][x + 1], + g[y][x + 2], + g[y + 1][x + 2], + g[y + 2][x + 2], + g[y + 2][x + 1], + g[y + 2][x], + g[y + 1][x], + ]; + + const i = cells.indexOf(2); + if (i === -1) return 0; + cells.push(...cells.splice(0, i)); + + const circle = [2, 9, 4, 3, 8, 1, 6, 7]; + const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9]; + + if (cells.every((x, i) => x === circle[i])) return 1; + if (cells.every((x, i) => x === reverseCircle[i])) return 1; + + return 0; + }; + + let res = 0; + for (let i = 0; i < m - 2; i++) { + for (let j = 0; j < n - 2; j++) { + res += check(i, j); + } + } + + return res; +} From b13ac11987a661012f98f4efa6601f2d5772a30f Mon Sep 17 00:00:00 2001 From: rain84 Date: Fri, 9 Aug 2024 20:43:58 +0300 Subject: [PATCH 2/4] feat: add js solution to lc problem: No.0840 --- .../0840.Magic Squares In Grid/README.md | 46 +++++++++++++++++++ .../0840.Magic Squares In Grid/README_EN.md | 46 +++++++++++++++++++ .../0840.Magic Squares In Grid/Solution2.js | 41 +++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 solution/0800-0899/0840.Magic Squares In Grid/Solution2.js diff --git a/solution/0800-0899/0840.Magic Squares In Grid/README.md b/solution/0800-0899/0840.Magic Squares In Grid/README.md index a7479e429c702..6396395e73de5 100644 --- a/solution/0800-0899/0840.Magic Squares In Grid/README.md +++ b/solution/0800-0899/0840.Magic Squares In Grid/README.md @@ -374,6 +374,52 @@ export function numMagicSquaresInside(grid: number[][]): number { } ``` +#### JavaScript + +```js +function numMagicSquaresInside(grid) { + const [m, n] = [grid.length, grid[0].length]; + if (m < 3 || n < 3) return 0; + + const check = (y, x) => { + const g = grid; + if (g[y + 1][x + 1] !== 5) return false; + + const cells = [ + g[y][x], + g[y][x + 1], + g[y][x + 2], + g[y + 1][x + 2], + g[y + 2][x + 2], + g[y + 2][x + 1], + g[y + 2][x], + g[y + 1][x], + ]; + + const i = cells.indexOf(2); + if (i === -1) return false; + cells.push(...cells.splice(0, i)); + + const circle = [2, 9, 4, 3, 8, 1, 6, 7]; + const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9]; + + if (cells.every((x, i) => x === circle[i])) return true; + if (cells.every((x, i) => x === reverseCircle[i])) return true; + + return false; + }; + + let res = 0; + for (let i = 0; i < m - 2; i++) { + for (let j = 0; j < n - 2; j++) { + res += +check(i, j); + } + } + + return res; +} +``` + diff --git a/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md b/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md index 2d1b7ca524d09..4ccb19bc4d635 100644 --- a/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md +++ b/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md @@ -370,6 +370,52 @@ export function numMagicSquaresInside(grid: number[][]): number { } ``` +#### JavaScript + +```js +function numMagicSquaresInside(grid) { + const [m, n] = [grid.length, grid[0].length]; + if (m < 3 || n < 3) return 0; + + const check = (y, x) => { + const g = grid; + if (g[y + 1][x + 1] !== 5) return false; + + const cells = [ + g[y][x], + g[y][x + 1], + g[y][x + 2], + g[y + 1][x + 2], + g[y + 2][x + 2], + g[y + 2][x + 1], + g[y + 2][x], + g[y + 1][x], + ]; + + const i = cells.indexOf(2); + if (i === -1) return false; + cells.push(...cells.splice(0, i)); + + const circle = [2, 9, 4, 3, 8, 1, 6, 7]; + const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9]; + + if (cells.every((x, i) => x === circle[i])) return true; + if (cells.every((x, i) => x === reverseCircle[i])) return true; + + return false; + }; + + let res = 0; + for (let i = 0; i < m - 2; i++) { + for (let j = 0; j < n - 2; j++) { + res += +check(i, j); + } + } + + return res; +} +``` + diff --git a/solution/0800-0899/0840.Magic Squares In Grid/Solution2.js b/solution/0800-0899/0840.Magic Squares In Grid/Solution2.js new file mode 100644 index 0000000000000..b93fe320dce7b --- /dev/null +++ b/solution/0800-0899/0840.Magic Squares In Grid/Solution2.js @@ -0,0 +1,41 @@ +function numMagicSquaresInside(grid) { + const [m, n] = [grid.length, grid[0].length]; + if (m < 3 || n < 3) return 0; + + const check = (y, x) => { + const g = grid; + if (g[y + 1][x + 1] !== 5) return false; + + const cells = [ + g[y][x], + g[y][x + 1], + g[y][x + 2], + g[y + 1][x + 2], + g[y + 2][x + 2], + g[y + 2][x + 1], + g[y + 2][x], + g[y + 1][x], + ]; + + const i = cells.indexOf(2); + if (i === -1) return false; + cells.push(...cells.splice(0, i)); + + const circle = [2, 9, 4, 3, 8, 1, 6, 7]; + const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9]; + + if (cells.every((x, i) => x === circle[i])) return true; + if (cells.every((x, i) => x === reverseCircle[i])) return true; + + return false; + }; + + let res = 0; + for (let i = 0; i < m - 2; i++) { + for (let j = 0; j < n - 2; j++) { + res += +check(i, j); + } + } + + return res; +} From 0214ea99077dff783b6e9aa3410b5a96118ddfc8 Mon Sep 17 00:00:00 2001 From: rain84 Date: Fri, 9 Aug 2024 20:48:04 +0300 Subject: [PATCH 3/4] feat: add js solution to lc problem: No.0840 --- .../0840.Magic Squares In Grid/README.md | 50 +++++++++++++++++++ .../0840.Magic Squares In Grid/README_EN.md | 50 +++++++++++++++++++ .../0840.Magic Squares In Grid/Solution.js | 45 +++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 solution/0800-0899/0840.Magic Squares In Grid/Solution.js diff --git a/solution/0800-0899/0840.Magic Squares In Grid/README.md b/solution/0800-0899/0840.Magic Squares In Grid/README.md index 6396395e73de5..86c3b8683a7ad 100644 --- a/solution/0800-0899/0840.Magic Squares In Grid/README.md +++ b/solution/0800-0899/0840.Magic Squares In Grid/README.md @@ -318,6 +318,56 @@ function numMagicSquaresInside(grid: number[][]): number { } ``` +#### JavaScript + +```js +function numMagicSquaresInside(grid) { + const m = grid.length; + const n = grid[0].length; + const check = (i, j) => { + if (i + 3 > m || j + 3 > n) { + return 0; + } + const cnt = Array(16).fill(0); + const row = Array(3).fill(0); + const col = Array(3).fill(0); + let [a, b] = [0, 0]; + for (let x = i; x < i + 3; ++x) { + for (let y = j; y < j + 3; ++y) { + const v = grid[x][y]; + if (v < 1 || v > 9 || ++cnt[v] > 1) { + return 0; + } + row[x - i] += v; + col[y - j] += v; + if (x - i === y - j) { + a += v; + } + if (x - i === 2 - (y - j)) { + b += v; + } + } + } + if (a !== b) { + return 0; + } + for (let k = 0; k < 3; ++k) { + if (row[k] !== a || col[k] !== a) { + return 0; + } + } + return 1; + }; + let ans = 0; + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + ans += check(i, j); + } + } + return ans; +} +``` + diff --git a/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md b/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md index 4ccb19bc4d635..61b63cb9090ae 100644 --- a/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md +++ b/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md @@ -314,6 +314,56 @@ function numMagicSquaresInside(grid: number[][]): number { } ``` +#### JavaScript + +```js +function numMagicSquaresInside(grid) { + const m = grid.length; + const n = grid[0].length; + const check = (i, j) => { + if (i + 3 > m || j + 3 > n) { + return 0; + } + const cnt = Array(16).fill(0); + const row = Array(3).fill(0); + const col = Array(3).fill(0); + let [a, b] = [0, 0]; + for (let x = i; x < i + 3; ++x) { + for (let y = j; y < j + 3; ++y) { + const v = grid[x][y]; + if (v < 1 || v > 9 || ++cnt[v] > 1) { + return 0; + } + row[x - i] += v; + col[y - j] += v; + if (x - i === y - j) { + a += v; + } + if (x - i === 2 - (y - j)) { + b += v; + } + } + } + if (a !== b) { + return 0; + } + for (let k = 0; k < 3; ++k) { + if (row[k] !== a || col[k] !== a) { + return 0; + } + } + return 1; + }; + let ans = 0; + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + ans += check(i, j); + } + } + return ans; +} +``` + diff --git a/solution/0800-0899/0840.Magic Squares In Grid/Solution.js b/solution/0800-0899/0840.Magic Squares In Grid/Solution.js new file mode 100644 index 0000000000000..fc08cda6431c1 --- /dev/null +++ b/solution/0800-0899/0840.Magic Squares In Grid/Solution.js @@ -0,0 +1,45 @@ +function numMagicSquaresInside(grid) { + const m = grid.length; + const n = grid[0].length; + const check = (i, j) => { + if (i + 3 > m || j + 3 > n) { + return 0; + } + const cnt = Array(16).fill(0); + const row = Array(3).fill(0); + const col = Array(3).fill(0); + let [a, b] = [0, 0]; + for (let x = i; x < i + 3; ++x) { + for (let y = j; y < j + 3; ++y) { + const v = grid[x][y]; + if (v < 1 || v > 9 || ++cnt[v] > 1) { + return 0; + } + row[x - i] += v; + col[y - j] += v; + if (x - i === y - j) { + a += v; + } + if (x - i === 2 - (y - j)) { + b += v; + } + } + } + if (a !== b) { + return 0; + } + for (let k = 0; k < 3; ++k) { + if (row[k] !== a || col[k] !== a) { + return 0; + } + } + return 1; + }; + let ans = 0; + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + ans += check(i, j); + } + } + return ans; +} From 269800236c09770b41432035126dcab06ff12c3e Mon Sep 17 00:00:00 2001 From: rain84 Date: Fri, 9 Aug 2024 17:49:14 +0000 Subject: [PATCH 4/4] style: format code and docs with prettier --- .../0840.Magic Squares In Grid/README.md | 74 +++++++++---------- .../0840.Magic Squares In Grid/README_EN.md | 74 +++++++++---------- 2 files changed, 74 insertions(+), 74 deletions(-) diff --git a/solution/0800-0899/0840.Magic Squares In Grid/README.md b/solution/0800-0899/0840.Magic Squares In Grid/README.md index 86c3b8683a7ad..2e9b56973f791 100644 --- a/solution/0800-0899/0840.Magic Squares In Grid/README.md +++ b/solution/0800-0899/0840.Magic Squares In Grid/README.md @@ -382,45 +382,45 @@ function numMagicSquaresInside(grid) { ```ts export function numMagicSquaresInside(grid: number[][]): number { - const [m, n] = [grid.length, grid[0].length] - if (m < 3 || n < 3) return 0 - - const check = (y: number, x: number) => { - const g = grid - if (g[y + 1][x + 1] !== 5) return 0 - - const cells = [ - g[y][x], - g[y][x + 1], - g[y][x + 2], - g[y + 1][x + 2], - g[y + 2][x + 2], - g[y + 2][x + 1], - g[y + 2][x], - g[y + 1][x], - ] - - const i = cells.indexOf(2) - if (i === -1) return 0 - cells.push(...cells.splice(0, i)) - - const circle = [2, 9, 4, 3, 8, 1, 6, 7] - const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9] - - if (cells.every((x, i) => x === circle[i])) return 1 - if (cells.every((x, i) => x === reverseCircle[i])) return 1 - - return 0 - } - - let res = 0 - for (let i = 0; i < m - 2; i++) { - for (let j = 0; j < n - 2; j++) { - res += check(i, j) + const [m, n] = [grid.length, grid[0].length]; + if (m < 3 || n < 3) return 0; + + const check = (y: number, x: number) => { + const g = grid; + if (g[y + 1][x + 1] !== 5) return 0; + + const cells = [ + g[y][x], + g[y][x + 1], + g[y][x + 2], + g[y + 1][x + 2], + g[y + 2][x + 2], + g[y + 2][x + 1], + g[y + 2][x], + g[y + 1][x], + ]; + + const i = cells.indexOf(2); + if (i === -1) return 0; + cells.push(...cells.splice(0, i)); + + const circle = [2, 9, 4, 3, 8, 1, 6, 7]; + const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9]; + + if (cells.every((x, i) => x === circle[i])) return 1; + if (cells.every((x, i) => x === reverseCircle[i])) return 1; + + return 0; + }; + + let res = 0; + for (let i = 0; i < m - 2; i++) { + for (let j = 0; j < n - 2; j++) { + res += check(i, j); + } } - } - return res + return res; } ``` diff --git a/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md b/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md index 61b63cb9090ae..70ee41f412e26 100644 --- a/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md +++ b/solution/0800-0899/0840.Magic Squares In Grid/README_EN.md @@ -378,45 +378,45 @@ function numMagicSquaresInside(grid) { ```ts export function numMagicSquaresInside(grid: number[][]): number { - const [m, n] = [grid.length, grid[0].length] - if (m < 3 || n < 3) return 0 - - const check = (y: number, x: number) => { - const g = grid - if (g[y + 1][x + 1] !== 5) return 0 - - const cells = [ - g[y][x], - g[y][x + 1], - g[y][x + 2], - g[y + 1][x + 2], - g[y + 2][x + 2], - g[y + 2][x + 1], - g[y + 2][x], - g[y + 1][x], - ] - - const i = cells.indexOf(2) - if (i === -1) return 0 - cells.push(...cells.splice(0, i)) - - const circle = [2, 9, 4, 3, 8, 1, 6, 7] - const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9] - - if (cells.every((x, i) => x === circle[i])) return 1 - if (cells.every((x, i) => x === reverseCircle[i])) return 1 - - return 0 - } - - let res = 0 - for (let i = 0; i < m - 2; i++) { - for (let j = 0; j < n - 2; j++) { - res += check(i, j) + const [m, n] = [grid.length, grid[0].length]; + if (m < 3 || n < 3) return 0; + + const check = (y: number, x: number) => { + const g = grid; + if (g[y + 1][x + 1] !== 5) return 0; + + const cells = [ + g[y][x], + g[y][x + 1], + g[y][x + 2], + g[y + 1][x + 2], + g[y + 2][x + 2], + g[y + 2][x + 1], + g[y + 2][x], + g[y + 1][x], + ]; + + const i = cells.indexOf(2); + if (i === -1) return 0; + cells.push(...cells.splice(0, i)); + + const circle = [2, 9, 4, 3, 8, 1, 6, 7]; + const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9]; + + if (cells.every((x, i) => x === circle[i])) return 1; + if (cells.every((x, i) => x === reverseCircle[i])) return 1; + + return 0; + }; + + let res = 0; + for (let i = 0; i < m - 2; i++) { + for (let j = 0; j < n - 2; j++) { + res += check(i, j); + } } - } - return res + return res; } ```