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
29 changes: 28 additions & 1 deletion src/count-islands/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
function countIslands(grid) {}
function countIslands(grid) {
// if(grid.length === 0 || grid === null) {
// return 0
// }

let count = 0
for(let row = 0; row < grid.length; row++) {
for(let col = 0; col < grid[row].length; col++) {
debugger;
if(grid[row][col] === 1) {
count++
search(grid, row, col)
}
}
}
return count

function search(grid, row, col) {
if(grid[row] === undefined || grid[row][col] === undefined || grid[row][col] === 0) return

grid[row][col] = 0

search(grid, row - 1, col)
search(grid, row + 1, col)
search(grid, row, col - 1)
search(grid, row, col + 1)
}
}

module.exports = countIslands;
10 changes: 4 additions & 6 deletions src/count-islands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ describe("Count Islands", () => {

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

expect(countIslands(grid)).toBe(4);
Expand Down
27 changes: 26 additions & 1 deletion src/word-search/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
function wordSearch(words, word) {}
function wordSearch(words, word) {
if(word === "") return true

for(let row = 0; row < words.length; row++) {
for(let col = 0; col < words[row].length; col++) {
if(words[row][col] === word[0]) {
if(search(0, row, col)) return true
}
}
}
return false

function search(index, row1, col1) {
if(index === word.length) return true
if(!words[row1] || !words[row1][col1]) return false
if(words[row1][col1] !== "#" && words[row1][col1] === word[index]) {
words[row1][col1] = "#"

if(search(index + 1, row1 - 1, col1)) return true
if(search(index + 1, row1 + 1, col1)) return true
if(search(index + 1, row1, col1 - 1)) return true
if(search(index + 1, row1, col1 + 1)) return true
}
return false
}
}

module.exports = wordSearch;