-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path221_maximal_square.js
67 lines (55 loc) · 1.57 KB
/
221_maximal_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* https://leetcode.com/problems/maximal-square/
* @param {character[][]} matrix
* @return {number}
*/
const checkAllZero = matrix => {
const height = matrix.length
const width = matrix[0].length
for (let i = 0; i < height; i++)
for (let j = 0; j < width; j++)
if (matrix[i][j] === '1')
return false
return true
}
const check = (matrix, x, y, size) => {
const height = matrix.length
const width = matrix[0].length
if (width < x + size) return false
if (height < y + size) return false
for (let nextX = x; nextX < x + size; nextX++) {
for (let nextY = y; nextY < y + size; nextY++) {
if (matrix[nextY][nextX] === "0") return false
if (matrix[nextY][nextX] === false) return false
}
}
return true
}
const updateMap = matrix => {
const height = matrix.length
const width = matrix[0].length
const map = Array(height - 1).fill().map(_ => Array(width - 1).fill(0))
let exits = false
for (let i = 0; i < height - 1; i++) {
for (let j = 0; j < width -1; j++) {
map[i][j] = check(matrix, j, i, 2)
exits |= map[i][j]
}
}
return exits ? map : null
}
var maximalSquare = function(matrix) {
const height = matrix.length
if (!height) return 0
const width = matrix[0].length
if (!width) return 0
if (checkAllZero(matrix)) return 0
let map = updateMap(matrix)
if (!map) return 1
let count = 1
while (map) {
map = updateMap(map)
count++
}
return count**2
};