-
Notifications
You must be signed in to change notification settings - Fork 1
/
number-of-islands.js
91 lines (81 loc) · 2.08 KB
/
number-of-islands.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Source: https://leetcode.com/problems/number-of-islands/
* Tags: [Depth-first Search,Breadth-first Search]
* Level: Medium
* Title: Number of Islands
* Auther: @imcoddy
* Content: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
*
* Example 1:
* 11110110101100000000
* Answer: 1
* Example 2:
* 11000110000010000011
* Answer: 3
*
* Credits:Special thanks to @mithmatt for adding this problem and creating all test cases.
*/
/**
* @param {character[][]} grid
* @return {number}
*/
/**
* Memo: BFS
* Complex: O(m*n)
* Runtime: 180ms
* Tests: 45 test cases passed
* Rank: B
*/
var numIslands = function(grid) {
var m = grid.length;
if (m <= 0) return 0;
var n = grid[0].length;
var count = 0;
for (var i = 0; i < m; i++) {
for (var j = 0; j < n; j++) {
if (grid[i][j] > 0) {
markIsland(i, j);
}
}
}
return count;
function markIsland(i, j) {
var directions = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0]
];
var queue = [
[i, j]
];
while (queue.length) {
var p = queue.pop();
grid[p[0]][p[1]] = -1 - count;
for (var i = 0; i < directions.length; i++) {
var next = [directions[i][0] + p[0], directions[i][1] + p[1]];
if (next[0] >= 0 && next[0] < m && next[1] >= 0 && next[1] < n && grid[next[0]][next[1]] > 0) {
queue.push(next);
}
}
}
count += 1;
}
};
var should = require('should');
console.time('Runtime');
numIslands([]).should.equal(0);
numIslands([
[]
]).should.equal(0);
numIslands([
[1, 1],
[1, 1],
[1, 0]
]).should.equal(1);
numIslands([
[1, 1, 0, 0],
[1, 1, 0, 1],
[1, 0, 1, 0]
]).should.equal(3);
console.timeEnd('Runtime');