-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path0695-max-area-of-island.js
More file actions
131 lines (108 loc) · 3.46 KB
/
0695-max-area-of-island.js
File metadata and controls
131 lines (108 loc) · 3.46 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
/**
* https://leetcode.com/problems/max-area-of-island
* Time O(ROWS * COLS) | Space O(ROWS * COLS)
* @param {number[][]} grid
* @return {number}
*/
var maxAreaOfIsland = function (grid, maxArea = 0) {
const [rows, cols] = [grid.length, grid[0].length];
const seen = new Array(rows).fill().map(() => new Array(cols));
for (let row = 0; row < rows; row++) {
/* Time O(ROWS) */
for (let col = 0; col < cols; col++) {
/* Time O(COLS) */
const area = getArea(
grid,
row,
rows,
col,
cols,
seen,
); /* Space O(ROWS * COLS) */
maxArea = Math.max(maxArea, area);
}
}
return maxArea;
};
var getArea = (grid, row, rows, col, cols, seen) => {
const isBaseCase = grid[row][col] === 0;
if (isBaseCase) return 0;
if (seen[row][col]) return 0;
seen[row][col] = true; /* Space O(ROWS * COLS) */
return dfs(grid, row, rows, col, cols, seen) + 1; /* Space O(ROWS * COLS) */
};
const dfs = (grid, row, rows, col, cols, seen, area = 0) => {
for (const [_row, _col] of getNeighbors(row, rows, col, cols)) {
area += getArea(grid, _row, rows, _col, cols, seen);
}
return area;
};
var getNeighbors = (row, rows, col, cols) =>
[
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
]
.map(([_row, _col]) => [row + _row, col + _col])
.filter(
([_row, _col]) =>
0 <= _row && _row < rows && 0 <= _col && _col < cols,
);
/**
* https://leetcode.com/problems/number-of-islands/
* Time O(ROWS * COLS) | Space O(ROWS * COLS)
* @param {character[][]} grid
* @return {number}
*/
var maxAreaOfIsland = (grid, maxArea = 0) => {
const [rows, cols] = [grid.length, grid[0].length];
const seen = new Array(rows).fill().map(() => new Array(cols));
for (let row = 0; row < rows; row++) {
/* Time O(ROWS) */
for (let col = 0; col < cols; col++) {
/* Time O(COLS) */
const isBaseCase = grid[row][col] === 0;
if (isBaseCase) continue;
if (seen[row][col]) continue;
seen[row][col] = true; /* Space O(ROWS * COLS) */
const area = getArea(
new Queue([[row, col]]),
grid,
seen,
); /* Space O(ROWS * COLS) */
maxArea = Math.max(maxArea, area);
}
}
return maxArea;
};
var getArea = (queue, grid, seen, area = 0) => {
const [rows, cols] = [grid.length, grid[0].length];
while (!queue.isEmpty()) {
for (let i = queue.size() - 1; 0 <= i; i--) {
/* Time O(WIDTH) */
const [row, col] = queue.dequeue();
for (const [_row, _col] of getNeighbors(row, rows, col, cols)) {
const isBaseCase = grid[_row][_col] === 0;
if (isBaseCase) continue;
if (seen[_row][_col]) continue;
seen[_row][_col] = true; /* Space O(ROWS * COLS) */
queue.enqueue([_row, _col]); /* Space O(HEIGHT) */
}
area++;
}
}
return area;
};
var getNeighbors = (row, rows, col, cols) =>
[
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
]
.map(([_row, _col]) => [row + _row, col + _col])
.filter(
([_row, _col]) =>
0 <= _row && _row < rows && 0 <= _col && _col < cols,
);