Skip to content

Commit ced6762

Browse files
authored
Merge pull request #880 from 0xff-dev/1219
Add solution and test-cases for problem 1219
2 parents 09a1dcb + f564eee commit ced6762

File tree

3 files changed

+75
-23
lines changed

3 files changed

+75
-23
lines changed

leetcode/1201-1300/1219.Path-with-Maximum-Gold/README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [1219.Path with Maximum Gold][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
Return the maximum amount of gold you can collect under the conditions:
7+
8+
- Every time you are located in a cell you will collect all the gold in that cell.
9+
- From your position, you can walk one step to the left, right, up, or down.
10+
- You can't visit the same cell more than once.
11+
- Never visit a cell with `0` gold.
12+
- You can start and stop collecting gold from **any** position in the grid that has some gold.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
18+
Output: 24
19+
Explanation:
20+
[[0,6,0],
21+
[5,8,7],
22+
[0,9,0]]
23+
Path to get the maximum gold, 9 -> 8 -> 7.
1324
```
1425

15-
## 题意
16-
> ...
26+
**Example 2:**
1727

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Path with Maximum Gold
23-
```go
2428
```
25-
29+
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
30+
Output: 28
31+
Explanation:
32+
[[1,0,7],
33+
[2,0,6],
34+
[3,4,5],
35+
[0,3,0],
36+
[9,0,20]]
37+
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
38+
```
2639

2740
## 结语
2841

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(grid [][]int) int {
4+
rows, cols := len(grid), len(grid[0])
5+
6+
var dfs func(int, int, [][]int) int
7+
dfs = func(x, y int, matrix [][]int) int {
8+
if x < 0 || x >= rows || y < 0 || y >= cols || matrix[x][y] == 0 {
9+
return 0
10+
}
11+
cur := matrix[x][y]
12+
matrix[x][y] = 0
13+
a := dfs(x-1, y, matrix)
14+
a = max(a, dfs(x+1, y, matrix))
15+
a = max(a, dfs(x, y-1, matrix))
16+
a = max(a, dfs(x, y+1, matrix))
17+
matrix[x][y] = cur
18+
19+
return cur + a
20+
}
21+
matrix := make([][]int, rows)
22+
for i := 0; i < rows; i++ {
23+
matrix[i] = make([]int, cols)
24+
}
25+
ans := 0
26+
for i := 0; i < rows; i++ {
27+
for j := 0; j < cols; j++ {
28+
29+
for k := 0; k < rows; k++ {
30+
copy(matrix[k], grid[k])
31+
}
32+
if grid[i][j] != 0 {
33+
ans = max(ans, dfs(i, j, matrix))
34+
}
35+
}
36+
}
37+
return ans
538
}

leetcode/1201-1300/1219.Path-with-Maximum-Gold/Solution_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{
17+
{0, 6, 0}, {5, 8, 7}, {0, 9, 0},
18+
}, 24},
19+
{"TestCase2", [][]int{
20+
{1, 0, 7}, {2, 0, 6}, {3, 4, 5}, {0, 3, 0}, {9, 0, 20},
21+
}, 28},
22+
{"TestCase3", [][]int{
23+
{1, 0, 7, 0, 0, 0}, {2, 0, 6, 0, 1, 0}, {3, 5, 6, 7, 4, 2}, {4, 3, 1, 0, 2, 0}, {3, 0, 5, 0, 20, 0},
24+
}, 60},
1925
}
2026

2127
// 开始测试
@@ -30,10 +36,10 @@ func TestSolution(t *testing.T) {
3036
}
3137
}
3238

33-
// 压力测试
39+
// 压力测试
3440
func BenchmarkSolution(b *testing.B) {
3541
}
3642

37-
// 使用案列
43+
// 使用案列
3844
func ExampleSolution() {
3945
}

0 commit comments

Comments
 (0)