(matrix)
| 71 | */ |
| 72 | |
| 73 | function removeIslands(matrix) { |
| 74 | // 1 -- change ones connected to border to twos '2' |
| 75 | |
| 76 | for (let row = 0; row < matrix.length; row++) { |
| 77 | for (let col = 0; col < matrix[row].length; col++) { |
| 78 | const rowIsBorder = row === 0 || row === matrix.length - 1; |
| 79 | const colIsBorder = col === 0 || col === matrix[row].length - 1; |
| 80 | const isBorder = rowIsBorder || colIsBorder; |
| 81 | if (!isBorder) continue; |
| 82 | if (matrix[row][col] !== 1) continue; |
| 83 | changeOnesConnectedToBorderToTwos(matrix, row, col); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // 2 -- change '2' to '1' and '1' to '0'; |
| 88 | |
| 89 | for (let row = 0; row < matrix.length; row++) { |
| 90 | for (let col = 0; col < matrix[row].length; col++) { |
| 91 | const color = matrix[row][col]; |
| 92 | if (color === 1) { |
| 93 | matrix[row][col] = 0; |
| 94 | } else if (color === 2) { |
| 95 | matrix[row][col] = 1; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return matrix; |
| 101 | } |
| 102 | |
| 103 | function changeOnesConnectedToBorderToTwos(matrix, startRow, startCol) { |
| 104 | const stack = [[startRow, startCol]]; |
no test coverage detected