Skip to content

Commit ae3b7c5

Browse files
committed
+ problem 1219
1 parent 2d2b2bd commit ae3b7c5

File tree

5 files changed

+228
-0
lines changed

5 files changed

+228
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# 1219. Path with Maximum Gold
2+
In a gold mine `grid` of size `m x n`, each cell in this mine has an integer representing the amount of gold in that cell, `0` if it is empty.
3+
4+
Return the maximum amount of gold you can collect under the conditions:
5+
* Every time you are located in a cell you will collect all the gold in that cell.
6+
* From your position, you can walk one step to the left, right, up, or down.
7+
* You can't visit the same cell more than once.
8+
* Never visit a cell with `0` gold.
9+
* You can start and stop collecting gold from **any** position in the grid that has some gold.
10+
11+
#### Example 1:
12+
<pre>
13+
<strong>Input:</strong> grid = [[0,6,0],[5,8,7],[0,9,0]]
14+
<strong>Output:</strong> 24
15+
<strong>Explanation:</strong>
16+
[[0,6,0],
17+
[5,8,7],
18+
[0,9,0]]
19+
Path to get the maximum gold, 9 -> 8 -> 7.
20+
</pre>
21+
22+
#### Example 2:
23+
<pre>
24+
<strong>Input:</strong> grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
25+
<strong>Output:</strong> 28
26+
<strong>Explanation:</strong>
27+
[[1,0,7],
28+
[2,0,6],
29+
[3,4,5],
30+
[0,3,0],
31+
[9,0,20]]
32+
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
33+
</pre>
34+
35+
#### Constraints:
36+
* `m == grid.length`
37+
* `n == grid[i].length`
38+
* `1 <= m, n <= 15`
39+
* `0 <= grid[i][j] <= 100`
40+
* There are at most **25** cells containing gold.
41+
42+
## Solutions (Python)
43+
44+
### 1. Solution
45+
```Python
46+
class Solution:
47+
def getMaximumGold(self, grid: List[List[int]]) -> int:
48+
m, n = len(grid), len(grid[0])
49+
gold = 0
50+
ret = 0
51+
52+
def dfs(i: int, j: int) -> None:
53+
nonlocal gold, ret
54+
ret = max(ret, gold)
55+
56+
if i > 0 and grid[i - 1][j] > 0:
57+
gold += grid[i - 1][j]
58+
grid[i - 1][j] = -grid[i - 1][j]
59+
dfs(i - 1, j)
60+
gold += grid[i - 1][j]
61+
grid[i - 1][j] = -grid[i - 1][j]
62+
if i + 1 < m and grid[i + 1][j] > 0:
63+
gold += grid[i + 1][j]
64+
grid[i + 1][j] = -grid[i + 1][j]
65+
dfs(i + 1, j)
66+
gold += grid[i + 1][j]
67+
grid[i + 1][j] = -grid[i + 1][j]
68+
if j > 0 and grid[i][j - 1] > 0:
69+
gold += grid[i][j - 1]
70+
grid[i][j - 1] = -grid[i][j - 1]
71+
dfs(i, j - 1)
72+
gold += grid[i][j - 1]
73+
grid[i][j - 1] = -grid[i][j - 1]
74+
if j + 1 < n and grid[i][j + 1] > 0:
75+
gold += grid[i][j + 1]
76+
grid[i][j + 1] = -grid[i][j + 1]
77+
dfs(i, j + 1)
78+
gold += grid[i][j + 1]
79+
grid[i][j + 1] = -grid[i][j + 1]
80+
81+
for i in range(m):
82+
for j in range(n):
83+
if grid[i][j] > 0:
84+
gold = grid[i][j]
85+
grid[i][j] = -grid[i][j]
86+
dfs(i, j)
87+
grid[i][j] = -grid[i][j]
88+
89+
return ret
90+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 1219. 黄金矿工
2+
你要开发一座金矿,地质勘测学家已经探明了这座金矿中的资源分布,并用大小为 `m * n` 的网格 `grid` 进行了标注。每个单元格中的整数就表示这一单元格中的黄金数量;如果该单元格是空的,那么就是 `0`
3+
4+
为了使收益最大化,矿工需要按以下规则来开采黄金:
5+
* 每当矿工进入一个单元,就会收集该单元格中的所有黄金。
6+
* 矿工每次可以从当前位置向上下左右四个方向走。
7+
* 每个单元格只能被开采(进入)一次。
8+
* 不得开采(进入)黄金数目为 `0` 的单元格。
9+
* 矿工可以从网格中 **任意一个** 有黄金的单元格出发或者是停止。
10+
11+
#### 示例 1:
12+
<pre>
13+
<strong>输入:</strong> grid = [[0,6,0],[5,8,7],[0,9,0]]
14+
<strong>输出:</strong> 24
15+
<strong>解释:</strong>
16+
[[0,6,0],
17+
[5,8,7],
18+
[0,9,0]]
19+
一种收集最多黄金的路线是:9 -> 8 -> 7。
20+
</pre>
21+
22+
#### 示例 2:
23+
<pre>
24+
<strong>输入:</strong> grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
25+
<strong>输出:</strong> 28
26+
<strong>解释:</strong>
27+
[[1,0,7],
28+
[2,0,6],
29+
[3,4,5],
30+
[0,3,0],
31+
[9,0,20]]
32+
一种收集最多黄金的路线是:1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7。
33+
</pre>
34+
35+
#### 提示:
36+
* `1 <= grid.length, grid[i].length <= 15`
37+
* `0 <= grid[i][j] <= 100`
38+
* 最多 **25** 个单元格中有黄金。
39+
40+
## 题解 (Python)
41+
42+
### 1. 题解
43+
```Python
44+
class Solution:
45+
def getMaximumGold(self, grid: List[List[int]]) -> int:
46+
m, n = len(grid), len(grid[0])
47+
gold = 0
48+
ret = 0
49+
50+
def dfs(i: int, j: int) -> None:
51+
nonlocal gold, ret
52+
ret = max(ret, gold)
53+
54+
if i > 0 and grid[i - 1][j] > 0:
55+
gold += grid[i - 1][j]
56+
grid[i - 1][j] = -grid[i - 1][j]
57+
dfs(i - 1, j)
58+
gold += grid[i - 1][j]
59+
grid[i - 1][j] = -grid[i - 1][j]
60+
if i + 1 < m and grid[i + 1][j] > 0:
61+
gold += grid[i + 1][j]
62+
grid[i + 1][j] = -grid[i + 1][j]
63+
dfs(i + 1, j)
64+
gold += grid[i + 1][j]
65+
grid[i + 1][j] = -grid[i + 1][j]
66+
if j > 0 and grid[i][j - 1] > 0:
67+
gold += grid[i][j - 1]
68+
grid[i][j - 1] = -grid[i][j - 1]
69+
dfs(i, j - 1)
70+
gold += grid[i][j - 1]
71+
grid[i][j - 1] = -grid[i][j - 1]
72+
if j + 1 < n and grid[i][j + 1] > 0:
73+
gold += grid[i][j + 1]
74+
grid[i][j + 1] = -grid[i][j + 1]
75+
dfs(i, j + 1)
76+
gold += grid[i][j + 1]
77+
grid[i][j + 1] = -grid[i][j + 1]
78+
79+
for i in range(m):
80+
for j in range(n):
81+
if grid[i][j] > 0:
82+
gold = grid[i][j]
83+
grid[i][j] = -grid[i][j]
84+
dfs(i, j)
85+
grid[i][j] = -grid[i][j]
86+
87+
return ret
88+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution:
2+
def getMaximumGold(self, grid: List[List[int]]) -> int:
3+
m, n = len(grid), len(grid[0])
4+
gold = 0
5+
ret = 0
6+
7+
def dfs(i: int, j: int) -> None:
8+
nonlocal gold, ret
9+
ret = max(ret, gold)
10+
11+
if i > 0 and grid[i - 1][j] > 0:
12+
gold += grid[i - 1][j]
13+
grid[i - 1][j] = -grid[i - 1][j]
14+
dfs(i - 1, j)
15+
gold += grid[i - 1][j]
16+
grid[i - 1][j] = -grid[i - 1][j]
17+
if i + 1 < m and grid[i + 1][j] > 0:
18+
gold += grid[i + 1][j]
19+
grid[i + 1][j] = -grid[i + 1][j]
20+
dfs(i + 1, j)
21+
gold += grid[i + 1][j]
22+
grid[i + 1][j] = -grid[i + 1][j]
23+
if j > 0 and grid[i][j - 1] > 0:
24+
gold += grid[i][j - 1]
25+
grid[i][j - 1] = -grid[i][j - 1]
26+
dfs(i, j - 1)
27+
gold += grid[i][j - 1]
28+
grid[i][j - 1] = -grid[i][j - 1]
29+
if j + 1 < n and grid[i][j + 1] > 0:
30+
gold += grid[i][j + 1]
31+
grid[i][j + 1] = -grid[i][j + 1]
32+
dfs(i, j + 1)
33+
gold += grid[i][j + 1]
34+
grid[i][j + 1] = -grid[i][j + 1]
35+
36+
for i in range(m):
37+
for j in range(n):
38+
if grid[i][j] > 0:
39+
gold = grid[i][j]
40+
grid[i][j] = -grid[i][j]
41+
dfs(i, j)
42+
grid[i][j] = -grid[i][j]
43+
44+
return ret

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@
834834
[1210][1210l]|[Minimum Moves to Reach Target with Rotations][1210] |![rs]
835835
[1217][1217l]|[Play with Chips][1217] |![rs]
836836
[1218][1218l]|[Longest Arithmetic Subsequence of Given Difference][1218] |![rs]
837+
[1219][1219l]|[Path with Maximum Gold][1219] |![py]
837838
[1220][1220l]|[Count Vowels Permutation][1220] |![rs]
838839
[1221][1221l]|[Split a String in Balanced Strings][1221] |![rb]&nbsp;&nbsp;![rs]
839840
[1222][1222l]|[Queens That Can Attack the King][1222] |![rs]
@@ -2575,6 +2576,7 @@
25752576
[1210]:Problemset/1210-Minimum%20Moves%20to%20Reach%20Target%20with%20Rotations/README.md#1210-minimum-moves-to-reach-target-with-rotations
25762577
[1217]:Problemset/1217-Play%20with%20Chips/README.md#1217-play-with-chips
25772578
[1218]:Problemset/1218-Longest%20Arithmetic%20Subsequence%20of%20Given%20Difference/README.md#1218-longest-arithmetic-subsequence-of-given-difference
2579+
[1219]:Problemset/1219-Path%20with%20Maximum%20Gold/README.md#1219-path-with-maximum-gold
25782580
[1220]:Problemset/1220-Count%20Vowels%20Permutation/README.md#1220-count-vowels-permutation
25792581
[1221]:Problemset/1221-Split%20a%20String%20in%20Balanced%20Strings/README.md#1221-split-a-string-in-balanced-strings
25802582
[1222]:Problemset/1222-Queens%20That%20Can%20Attack%20the%20King/README.md#1222-queens-that-can-attack-the-king
@@ -4310,6 +4312,7 @@
43104312
[1210l]:https://leetcode.com/problems/minimum-moves-to-reach-target-with-rotations/
43114313
[1217l]:https://leetcode.com/problems/play-with-chips/
43124314
[1218l]:https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/
4315+
[1219l]:https://leetcode.com/problems/path-with-maximum-gold/
43134316
[1220l]:https://leetcode.com/problems/count-vowels-permutation/
43144317
[1221l]:https://leetcode.com/problems/split-a-string-in-balanced-strings/
43154318
[1222l]:https://leetcode.com/problems/queens-that-can-attack-the-king/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@
834834
[1210][1210l]|[穿过迷宫的最少移动次数][1210] |![rs]
835835
[1217][1217l]|[玩筹码][1217] |![rs]
836836
[1218][1218l]|[最长定差子序列][1218] |![rs]
837+
[1219][1219l]|[黄金矿工][1219] |![py]
837838
[1220][1220l]|[统计元音字母序列的数目][1220] |![rs]
838839
[1221][1221l]|[分割平衡字符串][1221] |![rb]&nbsp;&nbsp;![rs]
839840
[1222][1222l]|[可以攻击国王的皇后][1222] |![rs]
@@ -2575,6 +2576,7 @@
25752576
[1210]:Problemset/1210-Minimum%20Moves%20to%20Reach%20Target%20with%20Rotations/README_CN.md#1210-穿过迷宫的最少移动次数
25762577
[1217]:Problemset/1217-Play%20with%20Chips/README_CN.md#1217-玩筹码
25772578
[1218]:Problemset/1218-Longest%20Arithmetic%20Subsequence%20of%20Given%20Difference/README_CN.md#1218-最长定差子序列
2579+
[1219]:Problemset/1219-Path%20with%20Maximum%20Gold/README_CN.md#1219-黄金矿工
25782580
[1220]:Problemset/1220-Count%20Vowels%20Permutation/README_CN.md#1220-统计元音字母序列的数目
25792581
[1221]:Problemset/1221-Split%20a%20String%20in%20Balanced%20Strings/README_CN.md#1221-分割平衡字符串
25802582
[1222]:Problemset/1222-Queens%20That%20Can%20Attack%20the%20King/README_CN.md#1222-可以攻击国王的皇后
@@ -4310,6 +4312,7 @@
43104312
[1210l]:https://leetcode.cn/problems/minimum-moves-to-reach-target-with-rotations/
43114313
[1217l]:https://leetcode.cn/problems/play-with-chips/
43124314
[1218l]:https://leetcode.cn/problems/longest-arithmetic-subsequence-of-given-difference/
4315+
[1219l]:https://leetcode.cn/problems/path-with-maximum-gold/
43134316
[1220l]:https://leetcode.cn/problems/count-vowels-permutation/
43144317
[1221l]:https://leetcode.cn/problems/split-a-string-in-balanced-strings/
43154318
[1222l]:https://leetcode.cn/problems/queens-that-can-attack-the-king/

0 commit comments

Comments
 (0)