Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e8bf636

Browse files
committed
Merge pull request #349 from 0xff-dev/2017
Add solution and test-cases for problem 2017
2 parents 452929c + 451cd27 commit e8bf636

File tree

6 files changed

+65
-23
lines changed

6 files changed

+65
-23
lines changed

‎leetcode/2001-2100/2017.Grid-Game/README.md‎

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
# [2017.Grid Game][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+
You are given a **0-indexed** 2D array `grid` of size `2 x n`, where `grid[r][c]` represents the number of points at position `(r, c)` on the matrix. Two robots are playing a game on this matrix.
5+
6+
Both robots initially start at `(0, 0)` and want to reach `(1, n-1)`. Each robot may only move to the **right** (`(r, c) to (r, c + 1)`) or **down** (`(r, c) to (r + 1, c)`).
7+
8+
At the start of the game, the **first** robot moves from `(0, 0)` to `(1, n-1)`, collecting all the points from the cells on its path. For all cells `(r, c)` traversed on the path, `grid[r][c]` is set to `0`. Then, the **second** robot moves from `(0, 0)` to `(1, n-1)`, collecting the points on its path. Note that their paths may intersect with one another.
9+
10+
The **first** robot wants to **minimize** the number of points collected by the **second** robot. In contrast, the **second** robot wants to **maximize** the number of points it collects. If both robots play **optimally**, return the **number of points** collected by the **second** robot.
11+
712

8-
**Example 1:**
13+
**Example 1:**
14+
![example1](./a1.png)
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: grid = [[2,5,4],[1,5,1]]
18+
Output: 4
19+
Explanation: The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.
20+
The cells visited by the first robot are set to 0.
21+
The second robot will collect 0 + 0 + 4 + 0 = 4 points.
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
25+
![example2](./a2.png)
1726

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Grid Game
23-
```go
2427
```
28+
Input: grid = [[3,3,1],[8,5,2]]
29+
Output: 4
30+
Explanation: The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.
31+
The cells visited by the first robot are set to 0.
32+
The second robot will collect 0 + 3 + 1 + 0 = 4 points.
33+
```
34+
35+
**Example 3:**
36+
![example2](./a3.png)
2537

38+
```
39+
Input: grid = [[1,3,1,15],[1,3,3,1]]
40+
Output: 7
41+
Explanation: The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.
42+
The cells visited by the first robot are set to 0.
43+
The second robot will collect 0 + 1 +たす 3 +たす 3 +たす 0 = 7 points.
44+
```
2645

2746
## 结语
2847

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(grid [][]int) int64 {
4+
column := len(grid[0])
5+
for col := 1; col < column; col++ {
6+
grid[0][col] += grid[0][col-1]
7+
}
8+
9+
for col := column - 2; col >= 0; col-- {
10+
grid[1][col] += grid[1][col+1]
11+
}
12+
13+
secondMax := int64(-1)
14+
for firstDown := 0; firstDown < column; firstDown++ {
15+
// 第一个机器人在哪个节点向下转动
16+
top := int64(grid[0][column-1] - grid[0][firstDown])
17+
down := int64(grid[1][0] - grid[1][firstDown])
18+
if top < down {
19+
top = down
20+
}
21+
if secondMax == -1 || top < secondMax {
22+
secondMax = top
23+
}
24+
}
25+
26+
return secondMax
527
}

‎leetcode/2001-2100/2017.Grid-Game/Solution_test.go‎

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

2122
// 开始测试
@@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
3031
}
3132
}
3233

33-
//压力测试
34+
//压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
//使用案列
38+
//使用案列
3839
func ExampleSolution() {
3940
}
2.31 KB
Loading[フレーム]
2.08 KB
Loading[フレーム]
4.73 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /