Skip to content

Commit 5ad7a08

Browse files
committed
Leetcode | Maximum Number of Fish in a Grid | Kotlin
1 parent 619af1e commit 5ad7a08

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 2658. Maximum Number of Fish in a Grid
2+
3+
You are given a 0-indexed 2D matrix `grid` of size `m x n`, where `(r, c)` represents:
4+
5+
- A land cell if `grid[r][c] = 0`, or
6+
- A water cell containing `grid[r][c]` fish, if `grid[r][c] > 0`.
7+
8+
A fisher can start at any water cell `(r, c)` and can do the following operations any number of times:
9+
10+
- Catch all the fish at cell `(r, c)`, or
11+
- Move to any adjacent water cell.
12+
13+
Return the maximum number of fish the fisher can catch if he chooses his starting cell optimally, or 0 if no water cell
14+
exists.
15+
16+
An adjacent cell of the cell `(r, c)`, is one of the cells `(r, c + 1)`, `(r, c - 1)`, `(r + 1, c)` or `(r - 1, c)` if it exists.
17+
18+
Example 1:
19+
20+
Input: grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
21+
Output: 7
22+
Explanation: The fisher can start at cell (1,3) and collect 3 fish, then move to cell (2,3) and collect 4 fish.
23+
24+
Example 2:
25+
26+
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
27+
Output: 1
28+
Explanation: The fisher can start at cells (0,0) or (3,3) and collect a single fish.
29+
30+
Constraints:
31+
32+
- `m == grid.length`
33+
- `n == grid[i].length`
34+
- `1 <= m, n <= 10`
35+
- `0 <= grid[i][j] <= 10`
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.devstromo.medium.p2658
2+
3+
class SolutionKt {
4+
fun findMaxFish(grid: Array<IntArray>): Int {
5+
var max = 0
6+
for (i in grid.indices) {
7+
for (j in grid[0].indices) {
8+
if (grid[i][j] != 0) {
9+
max = maxOf(max, explore(i, j, grid))
10+
}
11+
}
12+
}
13+
return max
14+
}
15+
16+
private fun explore(row: Int, col: Int, grid: Array<IntArray>): Int {
17+
if (row < 0 || col < 0 || row >= grid.size || col >= grid[0].size) return 0
18+
if (grid[row][col] == 0) return 0
19+
var total = grid[row][col]
20+
grid[row][col] = 0
21+
total += explore(row, col + 1, grid) +
22+
explore(row, col - 1, grid) +
23+
explore(row + 1, col, grid) +
24+
explore(row - 1, col, grid)
25+
return total
26+
}
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.devstromo.medium.p2658
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
6+
class SolutionKtUnitTest {
7+
private val solution = SolutionKt()
8+
9+
@Test
10+
fun `Test Maximum Number of Fish in a Grid`() {
11+
assertEquals(
12+
7, solution.findMaxFish(
13+
arrayOf(
14+
intArrayOf(0, 2, 1, 0),
15+
intArrayOf(4, 0, 0, 3),
16+
intArrayOf(1, 0, 0, 4),
17+
intArrayOf(0, 3, 2, 0)
18+
)
19+
)
20+
)
21+
22+
assertEquals(
23+
1, solution.findMaxFish(
24+
arrayOf(
25+
intArrayOf(1, 0, 0, 0),
26+
intArrayOf(0, 0, 0, 0),
27+
intArrayOf(0, 0, 0, 0),
28+
intArrayOf(0, 0, 0, 1)
29+
)
30+
)
31+
)
32+
}
33+
}

0 commit comments

Comments
 (0)