forked from adamespii/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_084.js
49 lines (45 loc) Β· 1.16 KB
/
problem_084.js
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
// https://leetcode.com/problems/number-of-islands/
//
// O(MN) Time complexity
// O(P) Space complexity
// M is number of rows in matrix, N is the number of columns in matrix, P is the number of islands in matrix
/**
* Returns the number of islands in the matrix
* @param {number[][]} matrix
* @return {number}
*/
function countNumIsland(matrix) {
return countIslandDFS(matrix);
}
/**
* Finds count using DFS
* @param {number[][]} matrix
* @return {number}
*/
function countIslandDFS(matrix) {
let count = 0;
for (let r = 0; r < matrix.length; r++) {
for (let c = 0; c < metrix[r].length; c++) {
if (matrix[r][c] === 1) {
islandCountHelper(matrix, r, c);
}
}
}
}
/**
* Depth first search helper function
* @param {number[][]} matrix
* @param {number} r
* @param {number} c
* @return {number}
*/
function islandCountHelper(matrix, r, c) {
if (r < 0 || c < 0 || r >= matrix.length || c >= matrix[0].length) return;
if (matrix[r][c] === 0) return;
// set to visited
matrix[r][c] = 0;
islandCountHelper(matrix, r - 1, c);
islandCountHelper(matrix, r + 1, c);
islandCountHelper(matrix, r, c - 1);
islandCountHelper(matrix, r, c + 1);
}