-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #672 from 0xff-dev/2658
Add solution and test-cases for problem 2658
- Loading branch information
Showing
5 changed files
with
88 additions
and
9 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
leetcode/2601-2700/2658.Maximum-Number-of-Fish-in-a-Grid/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
31 changes: 29 additions & 2 deletions
31
leetcode/2601-2700/2658.Maximum-Number-of-Fish-in-a-Grid/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.