-
Notifications
You must be signed in to change notification settings - Fork 0
/
max-square.js
50 lines (39 loc) · 974 Bytes
/
max-square.js
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
const maxSquare = (matrix) => {
const n = matrix.length;
const D = new Array(n + 1).fill(0).map(() => new Array(n + 1).fill(0));
let max = 0;
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n; j++) {
if (matrix[i - 1][j - 1] === 1) {
D[i][j] = Math.min(D[i - 1][j - 1], D[i][j - 1], D[i - 1][j]) + 1;
if (D[i][j] > max) {
max = D[i][j];
}
}
}
}
return max * max;
};
const maxRectangle = (matrix) => {
const n = matrix.length;
const D = new Array(n + 1).fill(0).map(() => new Array(n + 1).fill(0));
let max = 0;
for (let i = 1; i < n; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][j - 1] === 1) {
D[i][j] = D[i][j - 1] + D[i - 1][j];
if (D[i][j] > max) {
max = D[i][j];
}
}
}
}
return max;
};
const matrix = [
[1, 0, 1, 0, 0],
[1, 0, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 0, 0, 1, 0]
];
console.log(maxRectangle(matrix));