Skip to content

Commit 913b951

Browse files
committed
solve problem Max Area Of Island
1 parent 7b12b77 commit 913b951

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ All solutions will be accepted!
7979
|349|[Intersection Of Two Arrays](https://leetcode-cn.com/problems/intersection-of-two-arrays/description/)|[java/py/js](./algorithms/IntersectionOfTwoArrays)|Easy|
8080
|268|[Missing Number](https://leetcode-cn.com/problems/missing-number/description/)|[java/py/js](./algorithms/MissingNumber)|Easy|
8181
|788|[Rotated Digits](https://leetcode-cn.com/problems/rotated-digits/description/)|[java/py/js](./algorithms/RotatedDigits)|Easy|
82+
|695|[Max Area Of Island](https://leetcode-cn.com/problems/max-area-of-island/description/)|[java/py/js](./algorithms/MaxAreaOfIsland)|Easy|
8283

8384
# Database
8485
|#|Title|Solution|Difficulty|

algorithms/MaxAreaOfIsland/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Max Area Of Island
2+
This problem is easy to solve by DFS
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Solution {
2+
public int maxAreaOfIsland(int[][] grid) {
3+
int maxArea = 0;
4+
Map<String, Integer> dfsMap = new HashMap<String, Integer>();
5+
6+
for (int i = 0; i < grid.length; i++) {
7+
for (int j = 0; j < grid[0].length; j++) {
8+
if (grid[i][j] == 1 && dfsMap.get(String.valueOf(i) + "X" + String.valueOf(j)) == null) {
9+
int area = 0;
10+
List<Map<String, Integer>> stack = new ArrayList<Map<String, Integer>>();
11+
Map<String, Integer> origin = new HashMap<String, Integer>();
12+
origin.put("x", j);
13+
origin.put("y", i);
14+
stack.add(origin);
15+
16+
while (stack.size() > 0) {
17+
Map<String, Integer> point = stack.get(stack.size() - 1);
18+
stack.remove(stack.size() - 1);
19+
20+
int x = point.get("x"),
21+
y = point.get("y");
22+
23+
if (dfsMap.get(String.valueOf(y) + "X" + String.valueOf(x)) == null) {
24+
area++;
25+
dfsMap.put(String.valueOf(y) + "X" + String.valueOf(x), 1);
26+
27+
if (y >= 1 && grid[y - 1][x] == 1) {
28+
point = new HashMap<String, Integer>();
29+
point.put("x", x);
30+
point.put("y", y - 1);
31+
stack.add(point);
32+
}
33+
if (y + 1 < grid.length && grid[y + 1][x] == 1) {
34+
point = new HashMap<String, Integer>();
35+
point.put("x", x);
36+
point.put("y", y + 1);
37+
stack.add(point);
38+
}
39+
if (x >= 1 && grid[y][x - 1] == 1) {
40+
point = new HashMap<String, Integer>();
41+
point.put("x", x - 1);
42+
point.put("y", y);
43+
stack.add(point);
44+
}
45+
if (x + 1 < grid[0].length && grid[y][x + 1] == 1) {
46+
point = new HashMap<String, Integer>();
47+
point.put("x", x + 1);
48+
point.put("y", y);
49+
stack.add(point);
50+
}
51+
}
52+
}
53+
54+
maxArea = Math.max(maxArea, area);
55+
}
56+
}
57+
}
58+
return maxArea;
59+
}
60+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number}
4+
*/
5+
var maxAreaOfIsland = function(grid) {
6+
let maxArea = 0,
7+
dfsMap = {}
8+
9+
for (let i = 0; i < grid.length; i++) {
10+
for (let j = 0; j < grid[0].length; j++) {
11+
if (grid[i][j] === 1 && dfsMap[`${i}X${j}`] === undefined) {
12+
let stack = [{ x: j, y: i }],
13+
area = 0
14+
15+
while (stack.length > 0) {
16+
let point = stack.pop(),
17+
x = point['x'],
18+
y = point['y']
19+
20+
if (dfsMap[`${y}X${x}`] === undefined) {
21+
area++
22+
dfsMap[`${y}X${x}`] = 1
23+
24+
if (y >= 1 && grid[y - 1][x] === 1) stack.push({ x, y: y - 1 })
25+
if (y + 1 < grid.length && grid[y + 1][x] === 1) stack.push({ x, y: y + 1})
26+
if (x >= 1 && grid[y][x - 1] === 1) stack.push({ x: x - 1, y })
27+
if (x + 1 < grid[0].length && grid[y][x + 1] === 1) stack.push({ x: x + 1, y })
28+
}
29+
}
30+
31+
maxArea = Math.max(maxArea, area)
32+
}
33+
}
34+
}
35+
return maxArea
36+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution(object):
2+
def maxAreaOfIsland(self, grid):
3+
"""
4+
:type grid: List[List[int]]
5+
:rtype: int
6+
"""
7+
max_area = 0
8+
dfs_map = {}
9+
for i in range(0, len(grid)):
10+
for j in range(0, len(grid[0])):
11+
if grid[i][j] == 1 and dfs_map.get(str(i) + 'X' + str(j)) == None:
12+
stack = [{ 'i': i, 'j': j }]
13+
area = 0
14+
while len(stack) > 0:
15+
point = stack.pop()
16+
i = point['i']
17+
j = point['j']
18+
if dfs_map.get(str(i) + 'X' + str(j)) == 1:
19+
continue
20+
else:
21+
area += 1
22+
dfs_map[str(i) + 'X' + str(j)] = 1
23+
24+
if i >= 1 and grid[i - 1][j] == 1:
25+
stack.append({ 'i': i - 1, 'j': j })
26+
if i + 1 < len(grid) and grid[i + 1][j] == 1:
27+
stack.append({ 'i': i + 1, 'j': j })
28+
if j >= 1 and grid[i][j - 1] == 1:
29+
stack.append({ 'i': i, 'j': j - 1 })
30+
if j + 1 < len(grid[0]) and grid[i][j + 1] == 1:
31+
stack.append({ 'i': i, 'j': j + 1 })
32+
max_area = max(max_area, area)
33+
return max_area

0 commit comments

Comments
 (0)