Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions src/count-islands/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
function countIslands(grid) {}
/*
- use recursion method (method that calls itself)
- use depth first search

module.exports = countIslands;
- create a variable to hold the count
- figure out the edge cases
- if there are no islands then return 0;
- the first loop i will be used to keep track of the outer loop i.e. the rows
- the second loop j will be used to keep track of the inner loop i.e. the columns
- if the grid at any point has 1 it should increase the count;

- figure out how to keep track of the island
- find an island we change it to 0 to mark as done? i.e. modifying
*/

function countIslands(grid) {
let islandCount = 0;
let rows = grid.length
let columns = grid[0].length

if (rows === 0) {
return 0;
}

for (let i = 0; i < rows; i++) {
for (let j = 0; j < columns; j++) {
if (grid[i][j] === 1) {
checkIsland(i, j, grid)
islandCount++;
console.log(islandCount);

}
}
}
return islandCount;

function checkIsland(i, j, grid) {
if ((i < 0 || j < 0) || (i >= rows || j >= columns) || grid[i][j] !== 1) {
return;
}
grid[i][j] = 0;

checkIsland(i + 1, j, grid)
checkIsland(i - 1, j, grid)
checkIsland(i, j - 1, grid)
checkIsland(i, j + 1, grid)
}
};

module.exports = countIslands;
274 changes: 139 additions & 135 deletions src/count-islands/test.js
Original file line number Diff line number Diff line change
@@ -1,138 +1,142 @@
const countIslands = require(".");

describe("Count Islands", () => {
test("flat island", () => {
const grid = [[1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1]];

expect(countIslands(grid)).toBe(3);
});

test("no land in flat sight", () => {
const grid = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]];

expect(countIslands(grid)).toBe(0);
});

test("3x3 island", () => {
const grid = [
[1, 1, 0],
[0, 0, 1],
[1, 0, 1],
];

expect(countIslands(grid)).toBe(3);
});

test("4x4 island", () => {
const grid = [
[
[1, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
[0, 1, 0, 0],
],
];

expect(countIslands(grid)).toBe(4);
});

test("5x5 island", () => {
const grid = [
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[1, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[1, 0, 1, 0, 1],
];

expect(countIslands(grid)).toBe(6);
});

test("Example island - 1", () => {
const grid = [
[1, 1, 0, 0, 0],
[1, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 1],
];

expect(countIslands(grid)).toBe(3);
});

test("Example island - 2", () => {
const grid = [
[1, 1, 1, 1, 0],
[1, 1, 0, 1, 0],
[1, 1, 0, 0, 0],
[0, 0, 0, 0, 0],
];

expect(countIslands(grid)).toBe(1);
});

test("large island - 1", () => {
const grid = [
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 0, 0, 0, 1],
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
[1, 0, 0, 0, 1],
[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
];

expect(countIslands(grid)).toBe(9);
});

test("all lands - 5x18", () => {
const grid = [
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
];

expect(countIslands(grid)).toBe(1);
});

test("large lands - 20x9", () => {
const grid = [
[1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0],
[1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0],
[1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0],
[1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0],
];

expect(countIslands(grid)).toBe(10);
});
});
test("flat island", () => {
const grid = [
[1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1]
];

expect(countIslands(grid)).toBe(3);
});

test("no land in flat sight", () => {
const grid = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
];

expect(countIslands(grid)).toBe(0);
});

test("3x3 island", () => {
const grid = [
[1, 1, 0],
[0, 0, 1],
[1, 0, 1],
];

expect(countIslands(grid)).toBe(3);
});

test("4x4 island", () => {
const grid = [

[1, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
[0, 1, 0, 0],

];

expect(countIslands(grid)).toBe(4);
});

test("5x5 island", () => {
const grid = [
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[1, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[1, 0, 1, 0, 1],
];

expect(countIslands(grid)).toBe(6);
});

test("Example island - 1", () => {
const grid = [
[1, 1, 0, 0, 0],
[1, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 1],
];

expect(countIslands(grid)).toBe(3);
});

test("Example island - 2", () => {
const grid = [
[1, 1, 1, 1, 0],
[1, 1, 0, 1, 0],
[1, 1, 0, 0, 0],
[0, 0, 0, 0, 0],
];

expect(countIslands(grid)).toBe(1);
});

test("large island - 1", () => {
const grid = [
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 0, 0, 0, 1],
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
[1, 0, 0, 0, 1],
[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
];

expect(countIslands(grid)).toBe(9);
});

test("all lands - 5x18", () => {
const grid = [
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
];

expect(countIslands(grid)).toBe(1);
});

test("large lands - 20x9", () => {
const grid = [
[1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0],
[1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0],
[1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0],
[1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0],
];

expect(countIslands(grid)).toBe(10);
});
});
Loading