@@ -54,11 +54,11 @@ Return _the maximum area of an island_ is to return the vertex count of the larg
54
54
* There are two major ways to explore a ` connected component ` (island): ** Breadth-First Search** and ** Depth-First Search** .
55
55
* For ** Depth-First Search** , there are two ways to make it: ` Recursive ` and ` Iterative ` . So I will provide 3 solutions in total.
56
56
* When we traverse each ` connected components ` (island), we can:
57
- * Mark each found land as ` 8 ` which represents ` visited ` . Visited lands don't need to be visited again.
57
+ * Mark each found land as ` 8 ` which represents ` visited ` (or ` included ` in ` area ` ) . Visited lands don't need to be visited again.
58
58
* ** count** the lands of the island.
59
59
3 . After all lands on an island have been visited, look for the next non-visited land.
60
60
4 . Repeat the above two steps until all the lands have been ` visited ` .
61
- 5 . At last, return the max land count .
61
+ 5 . At last, return the ` max_area ` .
62
62
63
63
## Solution 1: 'Depth-First Search' by Recursion
64
64
![ ] ( ../../images/binary_tree_DFS_1.png )
@@ -85,70 +85,65 @@ Similar problem is `200. Number of Islands`, please click [Breadth-First Search
85
85
``` python
86
86
class Solution :
87
87
def __init__ (self ):
88
- self .max_land_count = 0
89
- self .land_count = 0
88
+ self .max_area = 0
89
+ self .area = 0
90
90
self .grid = None
91
91
92
92
def maxAreaOfIsland (self , grid : List[List[int ]]) -> int :
93
93
self .grid = grid
94
94
95
95
for i in range (len (grid)):
96
96
for j in range (len (grid[0 ])):
97
- if self .grid[i][j] == 1 :
98
- self .land_count = 0
99
-
97
+ if grid[i][j] == 1 :
98
+ self .area = 0
100
99
self .depth_first_search(i, j)
100
+ self .max_area = max (self .max_area, self .area)
101
101
102
- return self .max_land_count
103
-
102
+ return self .max_area
103
+
104
104
def depth_first_search (self , i , j ):
105
- if i < 0 or i >= len (self .grid):
106
- return
107
-
108
- if j < 0 or j >= len (self .grid[0 ]):
105
+ if i < 0 or j < 0 or i >= len (self .grid) or j >= len (self .grid[0 ]):
109
106
return
110
107
111
108
if self .grid[i][j] != 1 :
112
109
return
113
110
114
111
self .grid[i][j] = 8
115
-
116
- self .land_count += 1
117
-
118
- if self .land_count > self .max_land_count:
119
- self .max_land_count = self .land_count
120
-
121
- self .depth_first_search(i - 1 , j)
122
- self .depth_first_search(i, j + 1 )
123
- self .depth_first_search(i + 1 , j)
124
- self .depth_first_search(i, j - 1 )
112
+ self .area += 1
113
+
114
+ for m, n in [
115
+ (- 1 , 0 ),
116
+ (0 , - 1 ), (0 , 1 ),
117
+ (1 , 0 ),
118
+ ]:
119
+ self .depth_first_search(i + m, j + n)
125
120
```
126
121
127
122
# Java
128
123
``` java
129
124
class Solution {
130
125
int [][] grid;
131
- int maxLandCount = 0 ;
132
- int landCount = 0 ;
126
+ int maxArea = 0 ;
127
+ int area = 0 ;
133
128
134
129
public int maxAreaOfIsland (int [][] grid ) {
135
130
this . grid = grid;
136
131
137
132
for (var i = 0 ; i < grid. length; i++ ) {
138
133
for (var j = 0 ; j < grid[0 ]. length; j++ ) {
139
134
if (grid[i][j] == 1 ) {
140
- landCount = 0 ;
141
-
135
+ area = 0 ;
142
136
depthFirstSearch(i, j);
137
+ maxArea = Math . max(maxArea, area);
143
138
}
144
139
}
145
140
}
146
141
147
- return maxLandCount ;
142
+ return maxArea ;
148
143
}
149
144
150
145
void depthFirstSearch (int i , int j ) {
151
- if (i < 0 || i >= grid. length) {
146
+ if (i < 0 || i >= grid. length) {
152
147
return ;
153
148
}
154
149
@@ -161,12 +156,7 @@ class Solution {
161
156
}
162
157
163
158
grid[i][j] = 8 ;
164
-
165
- landCount++ ;
166
-
167
- if (landCount > maxLandCount) {
168
- maxLandCount = landCount;
169
- }
159
+ area++ ;
170
160
171
161
depthFirstSearch(i - 1 , j);
172
162
depthFirstSearch(i, j + 1 );
@@ -186,20 +176,20 @@ public:
186
176
for (auto i = 0; i < grid_.size(); i++) {
187
177
for (auto j = 0; j < grid_[0].size(); j++) {
188
178
if (grid_[i][j] == 1) {
189
- land_count_ = 0;
190
-
179
+ area_ = 0;
191
180
depth_first_search(i, j);
181
+ max_area_ = max(max_area_, area_);
192
182
}
193
183
}
194
184
}
195
185
196
- return max_land_count_ ;
186
+ return max_area_ ;
197
187
}
198
188
199
189
private:
200
190
vector<vector<int >> grid_;
201
- int max_land_count_ = 0 ;
202
- int land_count_ = 0 ;
191
+ int max_area_ = 0 ;
192
+ int area_ = 0 ;
203
193
204
194
void depth_first_search (int i, int j) {
205
195
if (
@@ -211,12 +201,7 @@ private:
211
201
}
212
202
213
203
grid_[i][j] = 8;
214
-
215
- land_count_++;
216
-
217
- if (land_count_ > max_land_count_) {
218
- max_land_count_ = land_count_;
219
- }
204
+ area_++;
220
205
221
206
depth_first_search(i - 1, j);
222
207
depth_first_search(i, j + 1);
@@ -229,24 +214,24 @@ private:
229
214
# JavaScript
230
215
```JavaScript
231
216
let grid
232
- let maxLandCount = 0
233
- let landCount
217
+ let maxArea = 0
218
+ let area
234
219
235
220
var maxAreaOfIsland = function (grid_) {
236
221
grid = grid_
237
- maxLandCount = 0
222
+ maxArea = 0
238
223
239
224
grid.forEach((row, i) => {
240
225
row.forEach((value, j) => {
241
226
if (value === 1) {
242
- landCount = 0
243
-
227
+ area = 0
244
228
depthFirstSearch(i, j)
229
+ maxArea = Math.max(maxArea, area)
245
230
}
246
231
})
247
232
})
248
233
249
- return maxLandCount
234
+ return maxArea
250
235
};
251
236
252
237
@@ -264,12 +249,7 @@ function depthFirstSearch(i, j) {
264
249
}
265
250
266
251
grid[i][j] = 8
267
-
268
- landCount++
269
-
270
- if (landCount > maxLandCount) {
271
- maxLandCount = landCount
272
- }
252
+ area++
273
253
274
254
depthFirstSearch(i - 1, j)
275
255
depthFirstSearch(i, j + 1)
@@ -280,12 +260,14 @@ function depthFirstSearch(i, j) {
280
260
281
261
# C#
282
262
``` c#
283
- public class Solution {
263
+ public class Solution
264
+ {
284
265
int [][] grid ;
285
- int maxLandCount = 0 ;
286
- int landCount = 0 ;
266
+ int maxArea = 0 ;
267
+ int area = 0 ;
287
268
288
- public int MaxAreaOfIsland (int [][] grid ) {
269
+ public int MaxAreaOfIsland (int [][] grid )
270
+ {
289
271
this .grid = grid ;
290
272
291
273
for (var i = 0 ; i < grid .Length ; i ++ )
@@ -294,14 +276,14 @@ public class Solution {
294
276
{
295
277
if (grid [i ][j ] == 1 )
296
278
{
297
- landCount = 0 ;
298
-
279
+ area = 0 ;
299
280
depthFirstSearch (i , j );
281
+ maxArea = Math .Max (maxArea , area );
300
282
}
301
283
}
302
284
}
303
285
304
- return maxLandCount ;
286
+ return maxArea ;
305
287
}
306
288
307
289
void depthFirstSearch (int i , int j )
@@ -316,12 +298,7 @@ public class Solution {
316
298
return ;
317
299
318
300
grid [i ][j ] = 8 ;
319
-
320
- landCount ++ ;
321
-
322
- if (landCount > maxLandCount ) {
323
- maxLandCount = landCount ;
324
- }
301
+ area ++ ;
325
302
326
303
depthFirstSearch (i - 1 , j );
327
304
depthFirstSearch (i , j + 1 );
@@ -335,25 +312,25 @@ public class Solution {
335
312
``` go
336
313
var (
337
314
grid [][]int
338
- maxLandCount int
339
- landCount int
315
+ maxArea int
316
+ area int
340
317
)
341
318
342
319
func maxAreaOfIsland (grid_ [][]int ) int {
343
320
grid = grid_
344
- maxLandCount = 0
321
+ maxArea = 0
345
322
346
323
for i , row := range grid {
347
324
for j , value := range row {
348
325
if value == 1 {
349
- landCount = 0
350
-
326
+ area = 0
351
327
depthFirstSearch (i, j)
328
+ maxArea = max (maxArea, area)
352
329
}
353
330
}
354
331
}
355
332
356
- return maxLandCount
333
+ return maxArea
357
334
}
358
335
359
336
func depthFirstSearch (i int , j int ) {
@@ -370,12 +347,7 @@ func depthFirstSearch(i int, j int) {
370
347
}
371
348
372
349
grid[i][j] = 8
373
-
374
- landCount++
375
-
376
- if landCount > maxLandCount {
377
- maxLandCount = landCount
378
- }
350
+ area++
379
351
380
352
depthFirstSearch (i - 1 , j)
381
353
depthFirstSearch (i, j + 1 )
@@ -388,20 +360,20 @@ func depthFirstSearch(i int, j int) {
388
360
``` Ruby
389
361
def max_area_of_island (grid )
390
362
@grid = grid
391
- @max_land_count = 0
392
- @land_count = 0
363
+ @max_area = 0
364
+ @area = 0
393
365
394
366
@grid .each_with_index do |row , i |
395
367
row.each_with_index do |value , j |
396
368
if value == 1
397
- @land_count = 0
398
-
369
+ @area = 0
399
370
depth_first_search(i, j)
371
+ @max_area = [@max_area , @area ].max
400
372
end
401
373
end
402
374
end
403
375
404
- @max_land_count
376
+ @max_area
405
377
end
406
378
407
379
def depth_first_search (i , j )
@@ -412,12 +384,7 @@ def depth_first_search(i, j)
412
384
return if @grid [i][j] != 1
413
385
414
386
@grid [i][j] = 8
415
-
416
- @land_count += 1
417
-
418
- if @land_count > @max_land_count
419
- @max_land_count = @land_count
420
- end
387
+ @area += 1
421
388
422
389
depth_first_search(i - 1 , j)
423
390
depth_first_search(i, j + 1 )
0 commit comments