当前位置: 首页 > 滚动 > >正文

LeetCode 2658. Maximum Number of Fish in a Grid-视点

来源:哔哩哔哩    时间:2023-05-02 11:02:41

You are given a 0-indexed 2D matrix gridof 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 0if 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:

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:

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.

Constraints:

m == grid.length

n == grid[i].length

1 <= m, n <= 10

0 <= grid[i][j] <= 10

DFS经典题目;

我是一直没做出来,因为用的静态变量。。。尴尬;

Runtime: 28 ms, faster than 12.44% of Java online submissions for Maximum Number of Fish in a Grid.

Memory Usage: 43.1 MB, less than 10.36% of Java online submissions for Maximum Number of Fish in a Grid.

X 关闭

推荐内容

最近更新

Copyright ©  2015-2022 起点制鞋网版权所有  备案号:皖ICP备2022009963号-12   联系邮箱: 39 60 29 14 2@qq.com