forked from ex01tus/leetcode-grind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxAreaOfIsland.java
More file actions
64 lines (52 loc) · 1.81 KB
/
MaxAreaOfIsland.java
File metadata and controls
64 lines (52 loc) · 1.81 KB
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
package graph;
import java.util.Deque;
import java.util.LinkedList;
/**
* Description: https://leetcode.com/problems/max-area-of-island
* Difficulty: Medium
* Time complexity: O(n)
* Space complexity: O(n)
*/
public class MaxAreaOfIsland {
private int[][] directions;
private int[][] visited;
public int maxAreaOfIsland(int[][] grid) {
directions = new int[][] {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
visited = new int[grid.length][grid[0].length];
int maxSpace = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1 && visited[i][j] == 0) {
int space = dfs(grid, new int[] {i, j});
maxSpace = Math.max(space, maxSpace);
}
}
}
return maxSpace;
}
private int dfs(int[][] grid, int[] start) {
Deque<int[]> stack = new LinkedList<>();
stack.push(start);
int count = 0;
while (!stack.isEmpty()) {
int[] current = stack.pop();
if (visited[current[0]][current[1]] == 0) {
visited[current[0]][current[1]] = 1;
stack.push(current);
for (int[] dir : directions) {
int x = current[0] + dir[0];
int y = current[1] + dir[1];
if (x >= 0 && y >= 0 && x < grid.length && y < grid[0].length
&& grid[x][y] == 1
&& visited[x][y] == 0) {
stack.push(new int[] {x, y});
}
}
} else if (visited[current[0]][current[1]] == 1) {
count++;
visited[current[0]][current[1]] = 2;
}
}
return count;
}
}