-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1895-largest-magic-square.js
More file actions
138 lines (125 loc) · 4.02 KB
/
1895-largest-magic-square.js
File metadata and controls
138 lines (125 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Largest Magic Square
* Time Complexity: O(R * C * min(R, C)^2)
* Space Complexity: O(R * C)
*/
var largestMagicSquare = function (grid) {
const totalRows = grid.length;
const totalCols = grid[0].length;
const rowPrefixSums = Array.from({ length: totalRows + 1 }, () =>
new Array(totalCols + 1).fill(0),
);
const colPrefixSums = Array.from({ length: totalRows + 1 }, () =>
new Array(totalCols + 1).fill(0),
);
for (let currentGridRow = 1; currentGridRow <= totalRows; currentGridRow++) {
for (
let currentGridCol = 1;
currentGridCol <= totalCols;
currentGridCol++
) {
rowPrefixSums[currentGridRow][currentGridCol] =
rowPrefixSums[currentGridRow][currentGridCol - 1] +
grid[currentGridRow - 1][currentGridCol - 1];
colPrefixSums[currentGridRow][currentGridCol] =
colPrefixSums[currentGridRow - 1][currentGridCol] +
grid[currentGridRow - 1][currentGridCol - 1];
}
}
const defaultMinSize = 1;
for (
let squareSideLength = Math.min(totalRows, totalCols);
squareSideLength >= 2;
squareSideLength--
) {
for (
let bottomRightGridRow = squareSideLength;
bottomRightGridRow <= totalRows;
bottomRightGridRow++
) {
for (
let bottomRightGridCol = squareSideLength;
bottomRightGridCol <= totalCols;
bottomRightGridCol++
) {
let isCurrentSquareMagic = true;
const targetMagicSum =
rowPrefixSums[bottomRightGridRow - squareSideLength + 1][
bottomRightGridCol
] -
rowPrefixSums[bottomRightGridRow - squareSideLength + 1][
bottomRightGridCol - squareSideLength
];
for (
let checkRowOffset = 0;
checkRowOffset < squareSideLength;
checkRowOffset++
) {
const currentRowIndex =
bottomRightGridRow - squareSideLength + 1 + checkRowOffset;
const currentRowSum =
rowPrefixSums[currentRowIndex][bottomRightGridCol] -
rowPrefixSums[currentRowIndex][
bottomRightGridCol - squareSideLength
];
if (currentRowSum !== targetMagicSum) {
isCurrentSquareMagic = false;
break;
}
}
if (!isCurrentSquareMagic) continue;
for (
let checkColOffset = 0;
checkColOffset < squareSideLength;
checkColOffset++
) {
const currentColIndex =
bottomRightGridCol - squareSideLength + 1 + checkColOffset;
const currentColSum =
colPrefixSums[bottomRightGridRow][currentColIndex] -
colPrefixSums[bottomRightGridRow - squareSideLength][
currentColIndex
];
if (currentColSum !== targetMagicSum) {
isCurrentSquareMagic = false;
break;
}
}
if (!isCurrentSquareMagic) continue;
let mainDiagonalAccumulator = 0;
for (
let diagElemIndex = 0;
diagElemIndex < squareSideLength;
diagElemIndex++
) {
mainDiagonalAccumulator +=
grid[bottomRightGridRow - squareSideLength + diagElemIndex][
bottomRightGridCol - squareSideLength + diagElemIndex
];
}
if (mainDiagonalAccumulator !== targetMagicSum) {
isCurrentSquareMagic = false;
}
if (!isCurrentSquareMagic) continue;
let antiDiagonalAccumulator = 0;
for (
let antiDiagElemIndex = 0;
antiDiagElemIndex < squareSideLength;
antiDiagElemIndex++
) {
antiDiagonalAccumulator +=
grid[bottomRightGridRow - squareSideLength + antiDiagElemIndex][
bottomRightGridCol - 1 - antiDiagElemIndex
];
}
if (antiDiagonalAccumulator !== targetMagicSum) {
isCurrentSquareMagic = false;
}
if (isCurrentSquareMagic) {
return squareSideLength;
}
}
}
}
return defaultMinSize;
};