Skip to content

Commit 0214ea9

Browse files
committed
feat: add js solution to lc problem: No.0840
1 parent b13ac11 commit 0214ea9

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

solution/0800-0899/0840.Magic Squares In Grid/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,56 @@ function numMagicSquaresInside(grid: number[][]): number {
318318
}
319319
```
320320

321+
#### JavaScript
322+
323+
```js
324+
function numMagicSquaresInside(grid) {
325+
const m = grid.length;
326+
const n = grid[0].length;
327+
const check = (i, j) => {
328+
if (i + 3 > m || j + 3 > n) {
329+
return 0;
330+
}
331+
const cnt = Array(16).fill(0);
332+
const row = Array(3).fill(0);
333+
const col = Array(3).fill(0);
334+
let [a, b] = [0, 0];
335+
for (let x = i; x < i + 3; ++x) {
336+
for (let y = j; y < j + 3; ++y) {
337+
const v = grid[x][y];
338+
if (v < 1 || v > 9 || ++cnt[v] > 1) {
339+
return 0;
340+
}
341+
row[x - i] += v;
342+
col[y - j] += v;
343+
if (x - i === y - j) {
344+
a += v;
345+
}
346+
if (x - i === 2 - (y - j)) {
347+
b += v;
348+
}
349+
}
350+
}
351+
if (a !== b) {
352+
return 0;
353+
}
354+
for (let k = 0; k < 3; ++k) {
355+
if (row[k] !== a || col[k] !== a) {
356+
return 0;
357+
}
358+
}
359+
return 1;
360+
};
361+
let ans = 0;
362+
for (let i = 0; i < m; ++i) {
363+
for (let j = 0; j < n; ++j) {
364+
ans += check(i, j);
365+
}
366+
}
367+
return ans;
368+
}
369+
```
370+
321371
<!-- tabs:end -->
322372

323373
<!-- solution:end -->

solution/0800-0899/0840.Magic Squares In Grid/README_EN.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,56 @@ function numMagicSquaresInside(grid: number[][]): number {
314314
}
315315
```
316316

317+
#### JavaScript
318+
319+
```js
320+
function numMagicSquaresInside(grid) {
321+
const m = grid.length;
322+
const n = grid[0].length;
323+
const check = (i, j) => {
324+
if (i + 3 > m || j + 3 > n) {
325+
return 0;
326+
}
327+
const cnt = Array(16).fill(0);
328+
const row = Array(3).fill(0);
329+
const col = Array(3).fill(0);
330+
let [a, b] = [0, 0];
331+
for (let x = i; x < i + 3; ++x) {
332+
for (let y = j; y < j + 3; ++y) {
333+
const v = grid[x][y];
334+
if (v < 1 || v > 9 || ++cnt[v] > 1) {
335+
return 0;
336+
}
337+
row[x - i] += v;
338+
col[y - j] += v;
339+
if (x - i === y - j) {
340+
a += v;
341+
}
342+
if (x - i === 2 - (y - j)) {
343+
b += v;
344+
}
345+
}
346+
}
347+
if (a !== b) {
348+
return 0;
349+
}
350+
for (let k = 0; k < 3; ++k) {
351+
if (row[k] !== a || col[k] !== a) {
352+
return 0;
353+
}
354+
}
355+
return 1;
356+
};
357+
let ans = 0;
358+
for (let i = 0; i < m; ++i) {
359+
for (let j = 0; j < n; ++j) {
360+
ans += check(i, j);
361+
}
362+
}
363+
return ans;
364+
}
365+
```
366+
317367
<!-- tabs:end -->
318368

319369
<!-- solution:end -->
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function numMagicSquaresInside(grid) {
2+
const m = grid.length;
3+
const n = grid[0].length;
4+
const check = (i, j) => {
5+
if (i + 3 > m || j + 3 > n) {
6+
return 0;
7+
}
8+
const cnt = Array(16).fill(0);
9+
const row = Array(3).fill(0);
10+
const col = Array(3).fill(0);
11+
let [a, b] = [0, 0];
12+
for (let x = i; x < i + 3; ++x) {
13+
for (let y = j; y < j + 3; ++y) {
14+
const v = grid[x][y];
15+
if (v < 1 || v > 9 || ++cnt[v] > 1) {
16+
return 0;
17+
}
18+
row[x - i] += v;
19+
col[y - j] += v;
20+
if (x - i === y - j) {
21+
a += v;
22+
}
23+
if (x - i === 2 - (y - j)) {
24+
b += v;
25+
}
26+
}
27+
}
28+
if (a !== b) {
29+
return 0;
30+
}
31+
for (let k = 0; k < 3; ++k) {
32+
if (row[k] !== a || col[k] !== a) {
33+
return 0;
34+
}
35+
}
36+
return 1;
37+
};
38+
let ans = 0;
39+
for (let i = 0; i < m; ++i) {
40+
for (let j = 0; j < n; ++j) {
41+
ans += check(i, j);
42+
}
43+
}
44+
return ans;
45+
}

0 commit comments

Comments
 (0)