Skip to content

Commit

Permalink
Merge pull request #672 from 0xff-dev/2658
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2658
  • Loading branch information
6boris authored Nov 7, 2023
2 parents 3bffe6f + 28b77fe commit 78cc0c7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 9 deletions.
43 changes: 43 additions & 0 deletions leetcode/2601-2700/2658.Maximum-Number-of-Fish-in-a-Grid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# [2658.Maximum Number of Fish in a Grid][title]

## Description
You are given a **0-indexed** 2D matrix `grid` of size `m x n`, where `(r, c)` represents:

- A **land** cell if `grid[r][c] = 0`, or
- A **water** cell containing `grid[r][c]` fish, if `grid[r][c] > 0`.

A fisher can start at any **water** cell `(r, c)` and can do the following operations any number of times:

- Catch all the fish at cell `(r, c)`, or
- Move to any adjacent **water** cell.

Return the **maximum** number of fish the fisher can catch if he chooses his starting cell optimally, or `0` if no water cell exists.

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.

**Example 1:**

![example1](./example.png)

```
Input: grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
Output: 7
Explanation: The fisher can start at cell (1,3) and collect 3 fish, then move to cell (2,3) and collect 4 fish.
```

**Example 2:**

![example2](./example2.png)

```
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
Output: 1
Explanation: The fisher can start at cells (0,0) or (3,3) and collect a single fish.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/maximum-number-of-fish-in-a-grid/
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(grid [][]int) int {
m, n := len(grid), len(grid[0])
var dfs func(int, int, [][]bool) int
dfs = func(x, y int, visited [][]bool) int {
if x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == 0 {
return 0
}
if visited[x][y] {
return 0
}
visited[x][y] = true
return grid[x][y] + dfs(x+1, y, visited) + dfs(x-1, y, visited) + dfs(x, y+1, visited) + dfs(x, y-1, visited)
}
ans := 0
for r := 0; r < m; r++ {
for c := 0; c < n; c++ {
if grid[r][c] == 0 {
continue
}
v := make([][]bool, m)
for i := 0; i < n; i++ {
v[i] = make([]bool, n)
}
if r := dfs(r, c, v); r > ans {
ans = r
}
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{
{0, 2, 1, 0},
{4, 0, 0, 3},
{1, 0, 0, 4},
{0, 3, 2, 0},
}, 7},
{"TestCase2", [][]int{
{1, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 1},
}, 1},
}

// 开始测试
Expand All @@ -30,10 +39,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 78cc0c7

Please sign in to comment.