Skip to content

Instantly share code, notes, and snippets.

@iammateus
Created July 5, 2022 15:20
Show Gist options
  • Save iammateus/73f7bf346ef9d9a0ad9591379061e663 to your computer and use it in GitHub Desktop.
Save iammateus/73f7bf346ef9d9a0ad9591379061e663 to your computer and use it in GitHub Desktop.
function createMatrix(rows: number, colls: number) : number[][] {
const matrix = [];
for (let i = 0; i < rows; i++) {
const row = [];
for (let j = 0; j < colls; j++) {
row.push(0);
}
matrix.push(row);
}
return matrix;
}
function isAtBoard(row: number, coll: number, maxRow: number, maxColl: number): boolean {
return (row === 0 || coll === 0 || row === maxRow || coll === maxColl);
}
type block = {
row: number,
coll: number
}
function copy(arr: any[]): any[] {
return JSON.parse(JSON.stringify(arr));
}
function merge(a: number[][], b: number[][]) : number[][] {
const rows = a.length;
const colls = a[0].length;
const matrix = [];
for (let i = 0; i < rows; i++) {
const row = [];
for (let j = 0; j < colls; j++) {
row.push(a[i][j] || b[i][j]);
}
matrix.push(row);
}
return matrix;
}
function applyRelatedToResult(
original: number[][],
oldResult: number[][],
current: block,
passed: string[] = [],
) : number[][] {
passed.push(`${current.row}|${current.coll}`);
const maxRow = original.length - 1;
const maxColl = original[0].length - 1;
let result = copy(oldResult);
result[current.row][current.coll] = 1;
const top = current.row - 1 >= 0 ? { row: current.row - 1, coll: current.coll } : null;
const left = current.coll - 1 >= 0 ? { row: current.row, coll: current.coll - 1 } : null;
const right = current.coll + 1 <= maxColl ? { row: current.row, coll: current.coll + 1 } : null;
const bottom = current.row + 1 <= maxRow ? { row: current.row + 1, coll: current.coll } : null;
if (top
&& !isAtBoard(top.row, top.coll, maxRow, maxColl)
&& original[top.row][top.coll] === 1
&& !passed.includes(`${top.row}|${top.coll}`)) {
passed.push(`${top.row}|${top.coll}`);
result = merge(result, applyRelatedToResult(original, result, top, passed));
}
if (left
&& !isAtBoard(left.row, left.coll, maxRow, maxColl)
&& original[left.row][left.coll] === 1
&& !passed.includes(`${left.row}|${left.coll}`)) {
passed.push(`${left.row}|${left.coll}`);
result = merge(result, applyRelatedToResult(original, result, left, passed));
}
if (right
&& !isAtBoard(right.row, right.coll, maxRow, maxColl)
&& original[right.row][right.coll] === 1
&& !passed.includes(`${right.row}|${right.coll}`)) {
passed.push(`${right.row}|${right.coll}`);
result = merge(result, applyRelatedToResult(original, result, right, passed));
}
if (bottom
&& !isAtBoard(bottom.row, bottom.coll, maxRow, maxColl)
&& original[bottom.row][bottom.coll] === 1
&& !passed.includes(`${bottom.row}|${bottom.coll}`)) {
passed.push(`${bottom.row}|${bottom.coll}`);
result = merge(result, applyRelatedToResult(original, result, bottom, passed));
}
return result;
}
export default function removeIslands(board: number[][]) : number[][] {
let result = createMatrix(board.length, board[0].length);
const maxRow = board.length - 1;
const maxColl = board[0].length - 1;
for (let row = 0; row < board.length; row++) {
for (let coll = 0; coll < board.length; coll++) {
const isBoard = board[row][coll] === 1
&& isAtBoard(row, coll, maxRow, maxColl);
if (isBoard) {
const current = { row, coll };
result = merge(result, applyRelatedToResult(board, result, current));
}
}
}
console.log({ board, result });
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment