-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber_of_islands.php
48 lines (40 loc) · 1.23 KB
/
number_of_islands.php
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
<?php
/**
* Number of Islands
* @link https://leetcode.com/problems/number-of-islands
*
* Approach:
* Iterate through each element,
* if current element is land ('1'), then call recursive function that marks all its adjacent nodes as visited.
* Note that when marking nodes as visited if you find another land ('1'), then you should mark its adjacent nodes as well.
*
* Time Complexity - O(n)
* Space Complexity - O(n)
*/
class Solution {
public function numIslands($grid)
{
$islands = 0;
for ($i = 0; $i < count($grid); $i++) {
for ($j = 0; $j < count($grid[0]); $j++) {
if ($grid[$i][$j] === '1') {
$islands++;
$this->markAsVisited($grid, $i, $j);
}
}
}
return $islands;
}
private function markAsVisited(&$grid, $i, $j)
{
$grid[$i][$j] = '2';
$directions = [[$i, $j - 1], [$i, $j + 1], [$i - 1, $j], [$i + 1, $j]];
for ($k = 0; $k < 4; $k++) {
$i = $directions[$k][0];
$j = $directions[$k][1];
if (isset($grid[$i][$j]) && $grid[$i][$j] === '1') {
$this->markAsVisited($grid, $i, $j);
}
}
}
}