Skip to content

Commit

Permalink
feat: add js/ts solutions to lc problem: No.0840 (#3393)
Browse files Browse the repository at this point in the history
  • Loading branch information
rain84 authored Aug 10, 2024
1 parent e5b5d95 commit 6df3dbf
Show file tree
Hide file tree
Showing 5 changed files with 431 additions and 0 deletions.
152 changes: 152 additions & 0 deletions solution/0800-0899/0840.Magic Squares In Grid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
152 changes: 152 additions & 0 deletions solution/0800-0899/0840.Magic Squares In Grid/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
45 changes: 45 additions & 0 deletions solution/0800-0899/0840.Magic Squares In Grid/Solution.js
Original file line number Diff line number Diff line change
@@ -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;
}
Loading

0 comments on commit 6df3dbf

Please sign in to comment.