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..2e9b56973f791 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,158 @@ 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; +} +``` + + + + + + + +### 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; +} +``` + +#### 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 5585d83cb0877..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 @@ -314,6 +314,158 @@ 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; +} +``` + + + + + + + +### 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; +} +``` + +#### 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/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; +} 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; +} 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; +}