Skip to content

Add solution and test-cases for problem 1219 #880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions leetcode/1201-1300/1219.Path-with-Maximum-Gold/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
# [1219.Path with Maximum Gold][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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.

Return the maximum amount of gold you can collect under the conditions:

- Every time you are located in a cell you will collect all the gold in that cell.
- From your position, you can walk one step to the left, right, up, or down.
- You can't visit the same cell more than once.
- Never visit a cell with `0` gold.
- You can start and stop collecting gold from **any** position in the grid that has some gold.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Path with Maximum Gold
```go
```

Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
```

## 结语

Expand Down
37 changes: 35 additions & 2 deletions leetcode/1201-1300/1219.Path-with-Maximum-Gold/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(grid [][]int) int {
rows, cols := len(grid), len(grid[0])

var dfs func(int, int, [][]int) int
dfs = func(x, y int, matrix [][]int) int {
if x < 0 || x >= rows || y < 0 || y >= cols || matrix[x][y] == 0 {
return 0
}
cur := matrix[x][y]
matrix[x][y] = 0
a := dfs(x-1, y, matrix)
a = max(a, dfs(x+1, y, matrix))
a = max(a, dfs(x, y-1, matrix))
a = max(a, dfs(x, y+1, matrix))
matrix[x][y] = cur

return cur + a
}
matrix := make([][]int, rows)
for i := 0; i < rows; i++ {
matrix[i] = make([]int, cols)
}
ans := 0
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {

for k := 0; k < rows; k++ {
copy(matrix[k], grid[k])
}
if grid[i][j] != 0 {
ans = max(ans, dfs(i, j, matrix))
}
}
}
return ans
}
20 changes: 13 additions & 7 deletions leetcode/1201-1300/1219.Path-with-Maximum-Gold/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ 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, 6, 0}, {5, 8, 7}, {0, 9, 0},
}, 24},
{"TestCase2", [][]int{
{1, 0, 7}, {2, 0, 6}, {3, 4, 5}, {0, 3, 0}, {9, 0, 20},
}, 28},
{"TestCase3", [][]int{
{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},
}, 60},
}

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

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

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading