-
Notifications
You must be signed in to change notification settings - Fork 0
/
695. Max Area of Island
166 lines (146 loc) · 5.77 KB
/
695. Max Area of Island
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
Given a non-empty 2D array grid of 0's and 1's.
An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical).
You may assume all four edges of the grid are surrounded by water.
Find the maximum area of an island in the given 2D array.
If there is no island, the maximum area is 0.
Example:
[[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,0,1,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Given the above grid, return 6.
Idea: found any not visited LAND and traverse the new island.
*/
class Solution {
final static int LAND = 1;
final static int WATER = 0;
public int maxAreaOfIsland(int[][] grid) {
if (grid.length == 0) {
return 0;
}
boolean[][] isVisited = new boolean[grid.length][grid[0].length];
int maxArea = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == LAND && !isVisited[i][j]) {
int currentIslandArea = traverseIslandAndCountLand(grid, isVisited, i, j);
maxArea = Math.max(maxArea, currentIslandArea);
}
}
}
return maxArea;
}
// depth first search
private int traverseIslandAndCountLand(int[][] grid, boolean[][] isVisited, int i, int j) {
if (isVisited[i][j] || grid[i][j] == WATER) {
return 0;
}
isVisited[i][j] = true;
int counter = 1;
if (isNewIslandLand(grid, isVisited, i + 1, j)) {
counter += traverseIslandAndCountLand(grid, isVisited, i + 1, j);
}
if (isNewIslandLand(grid, isVisited, i - 1, j)) {
counter += traverseIslandAndCountLand(grid, isVisited, i - 1, j);
}
if (isNewIslandLand(grid, isVisited, i, j + 1)) {
counter += traverseIslandAndCountLand(grid, isVisited, i, j + 1);
}
if (isNewIslandLand(grid, isVisited, i, j - 1)) {
counter += traverseIslandAndCountLand(grid, isVisited, i, j - 1);
}
return counter;
}
private boolean isNewIslandLand(int[][] grid, boolean[][] isVisited, int i, int j) {
return i >= 0 && i < grid.length &&
j >= 0 && j < grid[i].length &&
!isVisited[i][j] &&
grid[i][j] == LAND;
}
}
//--------------------------------------------------------------------------------------------------------------------
//-------------------------------------Breadth First Solution via Queue-----------------------------------------------
//--------------------------------------------------------------------------------------------------------------------
class Solution {
public int maxAreaOfIsland(int[][] grid) {
int maxArea = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == 1) {
int current = 0;
Queue<Point> queue = new ArrayDeque<Point>();
queue.add(new Point(i, j));
while (!queue.isEmpty()) {
Point point = queue.poll();
grid[point.i][point.j] = 0;
current++;
if (point.i + 1 < grid.length) {
if (grid[point.i + 1][point.j] == 1) {
if (!queue.contains(new Point(point.i + 1, point.j))) {
queue.add(new Point(point.i + 1, point.j));
}
}
}
if (point.j + 1 < grid[point.i].length) {
if (grid[point.i][point.j + 1] == 1) {
if (!queue.contains(new Point(point.i, point.j + 1))) {
queue.add(new Point(point.i, point.j + 1));
}
}
}
if (point.i - 1 >= 0) {
if (grid[point.i - 1][point.j] == 1) {
if (!queue.contains(new Point(point.i - 1, point.j))) {
queue.add(new Point(point.i - 1, point.j));
}
}
}
if (point.j - 1 >= 0) {
if (grid[point.i][point.j - 1] == 1) {
if (!queue.contains(new Point(point.i, point.j - 1))) {
queue.add(new Point(point.i, point.j - 1));
}
}
}
}
if (current > maxArea) {
maxArea = current;
}
}
}
}
return maxArea;
}
class Point {
int i;
int j;
int square;
public Point(int i, int j) {
this.i = i;
this.j = j;
}
public int getSquare() {
return square;
}
public void setSquare(int square) {
this.square = square;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return i == point.i &&
j == point.j;
}
@Override
public int hashCode() {
return Objects.hash(i, j);
}
}
}