Skip to content

Commit a6ffd52

Browse files
authored
Merge pull request halfrost#85 from frankegoesdown/1436-Cherry-Pickup-II
1436 cherry pickup ii
2 parents ebebf5c + e4a828a commit a6ffd52

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package leetcode
2+
3+
func cherryPickup(grid [][]int) int {
4+
m, n := len(grid), len(grid[0])
5+
old, new := make([]int, n*n), make([]int, n*n)
6+
for i := range old {
7+
old[i] = -0xffffff
8+
}
9+
old[n-1] = grid[0][0]+grid[0][n-1]
10+
11+
// dp
12+
for k:=1; k<m; k++ {
13+
for s:=0; s<n*n; s++ {
14+
new[s] = -0xffffff
15+
c1, c2 := s/n, s%n
16+
toadd := grid[k][c1]
17+
if c1 != c2 {
18+
toadd += grid[k][c2]
19+
}
20+
for _, d1 := range []int{1,0,-1} {
21+
for _, d2 := range []int{1,0,-1} {
22+
nc1, nc2 := c1+d1, c2+d2
23+
if nc1>=0 && nc1<n && nc2>=0 && nc2<n && old[nc1*n+nc2]>=0 {
24+
new[s] = max(new[s], old[nc1*n+nc2]+toadd)
25+
}
26+
}
27+
}
28+
}
29+
old, new = new, old
30+
}
31+
allmax := 0
32+
for _, v := range old {
33+
if v>allmax {
34+
allmax = v
35+
}
36+
}
37+
return allmax
38+
}
39+
40+
func max(a, b int) int {
41+
if a>b {
42+
return a
43+
}
44+
return b
45+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1436 struct {
9+
para1436
10+
ans1436
11+
}
12+
13+
type para1436 struct {
14+
grid [][]int
15+
}
16+
17+
type ans1436 struct {
18+
ans int
19+
}
20+
21+
func Test_Problem1436(t *testing.T) {
22+
23+
qs := []question1436{
24+
25+
{
26+
para1436{[][]int{
27+
{3, 1, 1},
28+
{2, 5, 1},
29+
{1, 5, 5},
30+
{2, 1, 1},
31+
}},
32+
ans1436{24},
33+
},
34+
35+
{
36+
para1436{[][]int{
37+
{1, 0, 0, 0, 0, 0, 1},
38+
{2, 0, 0, 0, 0, 3, 0},
39+
{2, 0, 9, 0, 0, 0, 0},
40+
{0, 3, 0, 5, 4, 0, 0},
41+
{1, 0, 2, 3, 0, 0, 6},
42+
}},
43+
ans1436{28},
44+
},
45+
{
46+
para1436{[][]int{
47+
{1, 0, 0, 3},
48+
{0, 0, 0, 3},
49+
{0, 0, 3, 3},
50+
{9, 0, 3, 3},
51+
}},
52+
ans1436{22},
53+
},
54+
{
55+
para1436{[][]int{
56+
{1, 1},
57+
{1, 1},
58+
}},
59+
ans1436{4},
60+
},
61+
}
62+
63+
fmt.Printf("------------------------Leetcode Problem 1436------------------------\n")
64+
65+
for _, q := range qs {
66+
_, p := q.ans1436, q.para1436
67+
fmt.Printf("【input】:%v ", p)
68+
fmt.Printf("【output】:%v \n", cherryPickup(p.grid))
69+
}
70+
fmt.Printf("\n\n\n")
71+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [1463. Cherry Pickup II](https://leetcode.com/problems/cherry-pickup-ii/)
2+
Given a rows x cols matrix grid representing a field of cherries. Each cell in grid represents the number of cherries that you can collect.
3+
4+
You have two robots that can collect cherries for you, Robot #1 is located at the top-left corner (0,0) , and Robot #2 is located at the top-right corner (0, cols-1) of the grid.
5+
6+
Return the maximum number of cherries collection using both robots by following the rules below:
7+
8+
From a cell (i,j), robots can move to cell (i+1, j-1) , (i+1, j) or (i+1, j+1).
9+
When any robot is passing through a cell, It picks it up all cherries, and the cell becomes an empty cell (0).
10+
When both robots stay on the same cell, only one of them takes the cherries.
11+
Both robots cannot move outside of the grid at any moment.
12+
Both robots should reach the bottom row in the grid.
13+
14+
## Example 1:
15+
```
16+
Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]
17+
Output: 24
18+
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
19+
Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12.
20+
Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12.
21+
Total of cherries: 12 + 12 = 24.
22+
```
23+
24+
## Example 2:
25+
```
26+
Input: grid = [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]]
27+
Output: 28
28+
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
29+
Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17.
30+
Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11.
31+
Total of cherries: 17 + 11 = 28.
32+
```
33+
34+
## Example 3:
35+
```
36+
Input: grid = [[1,0,0,3],[0,0,0,3],[0,0,3,3],[9,0,3,3]]
37+
Output: 22
38+
```
39+
40+
## Example 4:
41+
```
42+
Input: grid = [[1,1],[1,1]]
43+
Output: 4
44+
```

0 commit comments

Comments
 (0)