-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0695-max-area-of-island.swift
35 lines (29 loc) · 1.09 KB
/
0695-max-area-of-island.swift
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
class Solution {
func maxAreaOfIsland(_ grid: [[Int]]) -> Int {
let numRows = grid.count
let numColumns = grid[0].count
var grid = grid
var maxArea = 0
for row in 0..<numRows {
for column in 0..<numColumns {
guard grid[row][column] == 1 else { continue }
let area = dfs(row: row, column: column, grid: &grid)
maxArea = max(maxArea, area)
}
}
return maxArea
}
private func dfs(row: Int, column: Int, grid: inout [[Int]]) -> Int {
guard row >= 0,
row < grid.count,
column >= 0,
column < grid[0].count,
grid[row][column] == 1
else { return 0 }
grid[row][column] = 0
return 1 + dfs(row: row + 1, column: column, grid: &grid)
+ dfs(row: row - 1, column: column, grid: &grid)
+ dfs(row: row, column: column + 1, grid: &grid)
+ dfs(row: row, column: column - 1, grid: &grid)
}
}