-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0695.go
78 lines (69 loc) · 2.13 KB
/
0695.go
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
// Source: https://leetcode.com/problems/max-area-of-island
// Title: Max Area of Island
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given an m x n binary matrix grid. 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.
//
// The area of an island is the number of cells with a value 1 in the island.
//
// Return the maximum area of an island in grid. If there is no island, return 0.
//
// Example 1:
//
// https://assets.leetcode.com/uploads/2021/05/01/maxarea1-grid.jpg
//
// Input: grid = [[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]]
// Output: 6
// Explanation: The answer is not 11, because the island must be connected 4-directionally.
//
// Example 2:
//
// Input: grid = [[0,0,0,0,0,0,0,0]]
// Output: 0
//
// Constraints:
//
// m == grid.length
// n == grid[i].length
// 1 <= m, n <= 50
// grid[i][j] is either 0 or 1.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
func maxAreaOfIsland(grid [][]int) int {
res := 0
m := len(grid)
n := len(grid[0])
for i, row := range grid {
for j, v := range row {
if v == 1 {
res = _max(res, _getIslandArea(grid, m, n, i, j))
}
}
}
return res
}
func _getIslandArea(grid [][]int, m, n, i, j int) int {
res := 1
grid[i][j] = 0
if i > 0 && grid[i-1][j] == 1 {
res += _getIslandArea(grid, m, n, i-1, j)
}
if i < m-1 && grid[i+1][j] == 1 {
res += _getIslandArea(grid, m, n, i+1, j)
}
if j > 0 && grid[i][j-1] == 1 {
res += _getIslandArea(grid, m, n, i, j-1)
}
if j < n-1 && grid[i][j+1] == 1 {
res += _getIslandArea(grid, m, n, i, j+1)
}
return res
}
func _max(a, b int) int {
if a > b {
return a
}
return b
}