forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxAreaOfIsland.kt
56 lines (52 loc) · 2.01 KB
/
MaxAreaOfIsland.kt
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
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
/**
* 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.
* Return the maximum area of an island in grid. If there is no island, return 0.
* <img src="https://assets.leetcode.com/uploads/2021/05/01/maxarea1-grid.jpg" height="150" width="150"/>
* [Source](https://leetcode.com/problems/max-area-of-island)
*/
@UseCommentAsDocumentation
private fun maxAreaOfIsland(grid: Array<IntArray>): Int {
val seen = mutableSetOf<Pair<Int, Int>>()
var max = 0
for (row in 0..grid.lastIndex) {
for (col in 0..grid[0].lastIndex) {
max = maxOf(max, maxConnected(grid, row, col, seen))
}
}
return max
}
private fun maxConnected(grid: Array<IntArray>, row: Int, col: Int, seen: MutableSet<Pair<Int, Int>>): Int {
if (row > grid.lastIndex || row < 0 || col < 0 || col > grid[0].lastIndex) {
return 0
}
val point = Pair(row, col)
if (point in seen) {
return 0
}
seen.add(point)
if (grid[row][col] == 1) {
return 1 + maxConnected(grid, row - 1, col, seen)
.plus(maxConnected(grid, row + 1, col, seen))
.plus(maxConnected(grid, row, col + 1, seen))
.plus(maxConnected(grid, row, col - 1, seen))
}
return 0
}
fun main() {
maxAreaOfIsland(
arrayOf(
intArrayOf(0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0),
intArrayOf(0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0),
intArrayOf(0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0),
intArrayOf(0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0),
intArrayOf(0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0),
intArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0),
intArrayOf(0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0),
intArrayOf(0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0)
)
) shouldBe 6
}