-
Notifications
You must be signed in to change notification settings - Fork 2
/
MaxAreaOfIsland.java
52 lines (46 loc) · 1.57 KB
/
MaxAreaOfIsland.java
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
package com.haibo.yan.algorithm.array;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
/**
* https://leetcode.com/problems/max-area-of-island/
*/
public class MaxAreaOfIsland {
public int maxAreaOfIsland(int[][] grid) {
int r = grid.length;
int c = grid[0].length;
int[][] moves = new int[][]{
{0, -1},
{0, 1},
{-1, 0},
{1, 0}
};
int max = 0;
for (int i = 0; i < r; i++) {
for(int j = 0; j < c; j++) {
if (grid[i][j] == 1) {
Stack<List<Integer>> stack = new Stack<>();
stack.push(Arrays.asList(i, j));
grid[i][j] = -1;
int island = 1;
while (!stack.isEmpty()) {
List<Integer> cord = stack.pop();
for (int[] move : moves) {
int ni = cord.get(0) + move[0];
int nj = cord.get(1) + move[1];
if (ni >= 0 && ni < r && nj >= 0 && nj < c) {
if (grid[ni][nj] == 1) {
stack.add(Arrays.asList(ni, nj));
grid[ni][nj] = -1;
island++;
}
}
}
}
max = Integer.max(island, max);
}
}
}
return max;
}
}