-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-islands.js
More file actions
155 lines (129 loc) · 4.16 KB
/
remove-islands.js
File metadata and controls
155 lines (129 loc) · 4.16 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
////////////////////////////////////////////////////////////////////////////////////////////////////////
// REMOVE ISLANDS
// MY SOLUTION
/*
// time O(wh)
// space O(wh) where wh is the widht and height of the matrix
function removeIslands(matrix) {
const visited = matrix.map(row => row.map(col => false));
for (let col = 0; col < matrix[0].length; col++) {
if(matrix[0][col] === 1) {
markNonIslands(visited, matrix, 0, col);
}
}
for (let row = 1; row < matrix.length; row++) {
if(matrix[row][0] === 1) {
markNonIslands(visited, matrix, row, 0);
}
}
for (let col = 1; col < matrix[0].length; col++) {
if(matrix[matrix.length - 1][col] === 1){
markNonIslands(visited, matrix, matrix.length - 1, col);
}
}
for (let row = 1; row < matrix.length; row++) {
if(matrix[row][matrix[0].length - 1] === 1) {
markNonIslands(visited, matrix, row ,matrix.length - 1);
}
}
for (let row = 1; row < matrix.length; row++) {
for (let col = 1; col < matrix[0].length; col++) {
if(matrix[row][col] === 1 && visited[row][col] === false) {
matrix[row][col] = 0;
}
}
}
return matrix;
}
function markNonIslands(visited, matrix, row, col) {
const stack = [[row,col]];
while (stack.length > 0) {
const current = stack.pop();
const [row, col] = current;
if(visited[row][col]) continue;
if (matrix[row][col] === 1) {
visited[row][col] = true;
}
const neighbors = getNeighbors(matrix, row, col);
for (const neighbor of neighbors) {
const [row, col] = neighbor;
if(visited[row][col]) continue;
stack.push(neighbor);
}
}
}
function getNeighbors(matrix, row, col) {
const neighbors = [];
if(row - 1 > 0 && matrix[row - 1][col] === 1) neighbors.push([row - 1, col])
if(row + 1 < matrix.length && matrix[row + 1][col] === 1) neighbors.push([row + 1, col]);
if(col - 1 > 0 && matrix[row][col - 1] === 1) neighbors.push([row, col - 1])
if(col + 1 < matrix[0].length && matrix[row][col + 1] === 1) neighbors.push([row, col + 1]);
return neighbors;
}
*/
function removeIslands(matrix) {
// 1 -- change ones connected to border to twos '2'
for (let row = 0; row < matrix.length; row++) {
for (let col = 0; col < matrix[row].length; col++) {
const rowIsBorder = row === 0 || row === matrix.length - 1;
const colIsBorder = col === 0 || col === matrix[row].length - 1;
const isBorder = rowIsBorder || colIsBorder;
if (!isBorder) continue;
if (matrix[row][col] !== 1) continue;
changeOnesConnectedToBorderToTwos(matrix, row, col);
}
}
// 2 -- change '2' to '1' and '1' to '0';
for (let row = 0; row < matrix.length; row++) {
for (let col = 0; col < matrix[row].length; col++) {
const color = matrix[row][col];
if (color === 1) {
matrix[row][col] = 0;
} else if (color === 2) {
matrix[row][col] = 1;
}
}
}
return matrix;
}
function changeOnesConnectedToBorderToTwos(matrix, startRow, startCol) {
const stack = [[startRow, startCol]];
while (stack.length > 0) {
const currentPosition = stack.pop();
const [currentRow, currenCol] = currentPosition;
matrix[currentRow][currenCol] = 2;
const neighbors = getNeighbors(matrix, currentRow, currenCol);
for (const neighbor of neighbors) {
const [row, col] = neighbor;
if (matrix[row][col] !== 1) continue;
stack.push(neighbor);
}
}
}
function getNeighbors(matrix, row, col) {
const neighbors = [];
const numRows = matrix.length;
const numCols = matrix[row].length;
if (row - 1 >= 0) neighbors.push([row - 1, col]); //UP
if (row + 1 < numRows) neighbors.push([row + 1, col]); //DOWN
if (col - 1 >= 0) neighbors.push([row, col - 1]); //LEFT
if (col + 1 < numCols) neighbors.push([row, col + 1]); //RIGHT
return neighbors;
}
const input = [
[1, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 1],
[0, 0, 1, 0, 1, 0],
[1, 1, 0, 0, 1, 0],
[1, 0, 1, 1, 0, 0],
[1, 0, 0, 0, 0, 1],
];
// expected = [
// [1, 0, 0, 0, 0, 0],
// [0, 0, 0, 1, 1, 1],
// [0, 0, 0, 0, 1, 0],
// [1, 1, 0, 0, 1, 0],
// [1, 0, 0, 0, 0, 0],
// [1, 0, 0, 0, 0, 1],
// ];
console.table(removeIslands(input));