Skip to content

Commit d1b7d18

Browse files
committed
+ problem 2017
1 parent 4c11b8f commit d1b7d18

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

Problemset/2017-Grid Game/README.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 2017. Grid Game
2+
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.
3+
4+
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)`).
5+
6+
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.
7+
8+
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*.
9+
10+
#### Example 1:
11+
![](https://assets.leetcode.com/uploads/2021/09/08/a1.png)
12+
<pre>
13+
<strong>Input:</strong> grid = [[2,5,4],[1,5,1]]
14+
<strong>Output:</strong> 4
15+
<strong>Explanation:</strong> 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.
16+
The cells visited by the first robot are set to 0.
17+
The second robot will collect 0 + 0 + 4 + 0 = 4 points.
18+
</pre>
19+
20+
#### Example 2:
21+
![](https://assets.leetcode.com/uploads/2021/09/08/a2.png)
22+
<pre>
23+
<strong>Input:</strong> grid = [[3,3,1],[8,5,2]]
24+
<strong>Output:</strong> 4
25+
<strong>Explanation:</strong> 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.
26+
The cells visited by the first robot are set to 0.
27+
The second robot will collect 0 + 3 + 1 + 0 = 4 points.
28+
</pre>
29+
30+
#### Example 3:
31+
![](https://assets.leetcode.com/uploads/2021/09/08/a3.png)
32+
<pre>
33+
<strong>Input:</strong> grid = [[1,3,1,15],[1,3,3,1]]
34+
<strong>Output:</strong> 7
35+
<strong>Explanation:</strong> 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.
36+
The cells visited by the first robot are set to 0.
37+
The second robot will collect 0 + 1 + 3 + 3 + 0 = 7 points.
38+
</pre>
39+
40+
#### Constraints:
41+
* `grid.length == 2`
42+
* `n == grid[r].length`
43+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
44+
* <code>1 <= grid[r][c] <= 10<sup>5</sup></code>
45+
46+
## Solutions (Rust)
47+
48+
### 1. Solution
49+
```Rust
50+
impl Solution {
51+
pub fn grid_game(grid: Vec<Vec<i32>>) -> i64 {
52+
let n = grid[0].len();
53+
let mut prefix_sum = vec![0; n];
54+
let mut suffix_sum = vec![0; n];
55+
let mut ret = i64::MAX;
56+
57+
for i in 1..n {
58+
prefix_sum[i] = prefix_sum[i - 1] + grid[1][i - 1] as i64;
59+
suffix_sum[n - 1 - i] = suffix_sum[n - i] + grid[0][n - i] as i64;
60+
}
61+
62+
for i in 0..n {
63+
ret = ret.min(prefix_sum[i].max(suffix_sum[i]));
64+
}
65+
66+
ret
67+
}
68+
}
69+
```
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 2017. 网格游戏
2+
给你一个下标从 **0** 开始的二维数组 `grid` ,数组大小为 `2 x n` ,其中 `grid[r][c]` 表示矩阵中 `(r, c)` 位置上的点数。现在有两个机器人正在矩阵上参与一场游戏。
3+
4+
两个机器人初始位置都是 `(0, 0)` ,目标位置是 `(1, n-1)` 。每个机器人只会 **向右** (`(r, c)``(r, c + 1)`) 或 向下 (`(r, c)``(r + 1, c)`) 。
5+
6+
游戏开始,**第一个** 机器人从 `(0, 0)` 移动到 `(1, n-1)` ,并收集路径上单元格的全部点数。对于路径上所有单元格 `(r, c)` ,途经后 `grid[r][c]` 会重置为 `0` 。然后,**第二个** 机器人从 `(0, 0)` 移动到 `(1, n-1)` ,同样收集路径上单元的全部点数。注意,它们的路径可能会存在相交的部分。
7+
8+
**第一个** 机器人想要打击竞争对手,使 **第二个** 机器人收集到的点数 **最小化** 。与此相对,**第二个** 机器人想要 **最大化** 自己收集到的点数。两个机器人都发挥出自己的 **最佳水平** 的前提下,返回 **第二个** 机器人收集到的 **点数**
9+
10+
#### 示例 1:
11+
![](https://assets.leetcode.com/uploads/2021/09/08/a1.png)
12+
<pre>
13+
<strong>输入:</strong> grid = [[2,5,4],[1,5,1]]
14+
<strong>输出:</strong> 4
15+
<strong>解释:</strong> 第一个机器人的最佳路径如红色所示,第二个机器人的最佳路径如蓝色所示。
16+
第一个机器人访问过的单元格将会重置为 0 。
17+
第二个机器人将会收集到 0 + 0 + 4 + 0 = 4 个点。
18+
</pre>
19+
20+
#### 示例 2:
21+
![](https://assets.leetcode.com/uploads/2021/09/08/a2.png)
22+
<pre>
23+
<strong>输入:</strong> grid = [[3,3,1],[8,5,2]]
24+
<strong>输出:</strong> 4
25+
<strong>解释:</strong> 第一个机器人的最佳路径如红色所示,第二个机器人的最佳路径如蓝色所示。
26+
第一个机器人访问过的单元格将会重置为 0 。
27+
第二个机器人将会收集到 0 + 3 + 1 + 0 = 4 个点。
28+
</pre>
29+
30+
#### 示例 3:
31+
![](https://assets.leetcode.com/uploads/2021/09/08/a3.png)
32+
<pre>
33+
<strong>输入:</strong> grid = [[1,3,1,15],[1,3,3,1]]
34+
<strong>输出:</strong> 7
35+
<strong>解释:</strong> 第一个机器人的最佳路径如红色所示,第二个机器人的最佳路径如蓝色所示。
36+
第一个机器人访问过的单元格将会重置为 0 。
37+
第二个机器人将会收集到 0 + 1 + 3 + 3 + 0 = 7 个点。
38+
</pre>
39+
40+
#### 提示:
41+
* `grid.length == 2`
42+
* `n == grid[r].length`
43+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
44+
* <code>1 <= grid[r][c] <= 10<sup>5</sup></code>
45+
46+
## 题解 (Rust)
47+
48+
### 1. 题解
49+
```Rust
50+
impl Solution {
51+
pub fn grid_game(grid: Vec<Vec<i32>>) -> i64 {
52+
let n = grid[0].len();
53+
let mut prefix_sum = vec![0; n];
54+
let mut suffix_sum = vec![0; n];
55+
let mut ret = i64::MAX;
56+
57+
for i in 1..n {
58+
prefix_sum[i] = prefix_sum[i - 1] + grid[1][i - 1] as i64;
59+
suffix_sum[n - 1 - i] = suffix_sum[n - i] + grid[0][n - i] as i64;
60+
}
61+
62+
for i in 0..n {
63+
ret = ret.min(prefix_sum[i].max(suffix_sum[i]));
64+
}
65+
66+
ret
67+
}
68+
}
69+
```

Problemset/2017-Grid Game/Solution.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn grid_game(grid: Vec<Vec<i32>>) -> i64 {
3+
let n = grid[0].len();
4+
let mut prefix_sum = vec![0; n];
5+
let mut suffix_sum = vec![0; n];
6+
let mut ret = i64::MAX;
7+
8+
for i in 1..n {
9+
prefix_sum[i] = prefix_sum[i - 1] + grid[1][i - 1] as i64;
10+
suffix_sum[n - 1 - i] = suffix_sum[n - i] + grid[0][n - i] as i64;
11+
}
12+
13+
for i in 0..n {
14+
ret = ret.min(prefix_sum[i].max(suffix_sum[i]));
15+
}
16+
17+
ret
18+
}
19+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,7 @@
11971197
[2011][2011l]|[Final Value of Variable After Performing Operations][2011] |![py]&nbsp;&nbsp;![rs]
11981198
[2013][2013l]|[Detect Squares][2013] |![rs]
11991199
[2016][2016l]|[Maximum Difference Between Increasing Elements][2016] |![rs]
1200+
[2017][2017l]|[Grid Game][2017] |![rs]
12001201
[2022][2022l]|[Convert 1D Array Into 2D Array][2022] |![rs]
12011202
[2023][2023l]|[Number of Pairs of Strings With Concatenation Equal to Target][2023] |![py]
12021203
[2024][2024l]|[Maximize the Confusion of an Exam][2024] |![rs]
@@ -2700,6 +2701,7 @@
27002701
[2011]:Problemset/2011-Final%20Value%20of%20Variable%20After%20Performing%20Operations/README.md#2011-final-value-of-variable-after-performing-operations
27012702
[2013]:Problemset/2013-Detect%20Squares/README.md#2013-detect-squares
27022703
[2016]:Problemset/2016-Maximum%20Difference%20Between%20Increasing%20Elements/README.md#2016-maximum-difference-between-increasing-elements
2704+
[2017]:Problemset/2017-Grid%20Game/README.md#2017-grid-game
27032705
[2022]:Problemset/2022-Convert%201D%20Array%20Into%202D%20Array/README.md#2022-convert-1d-array-into-2d-array
27042706
[2023]:Problemset/2023-Number%20of%20Pairs%20of%20Strings%20With%20Concatenation%20Equal%20to%20Target/README.md#2023-number-of-pairs-of-strings-with-concatenation-equal-to-target
27052707
[2024]:Problemset/2024-Maximize%20the%20Confusion%20of%20an%20Exam/README.md#2024-maximize-the-confusion-of-an-exam
@@ -4202,6 +4204,7 @@
42024204
[2011l]:https://leetcode.com/problems/final-value-of-variable-after-performing-operations/
42034205
[2013l]:https://leetcode.com/problems/detect-squares/
42044206
[2016l]:https://leetcode.com/problems/maximum-difference-between-increasing-elements/
4207+
[2017l]:https://leetcode.com/problems/grid-game/
42054208
[2022l]:https://leetcode.com/problems/convert-1d-array-into-2d-array/
42064209
[2023l]:https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/
42074210
[2024l]:https://leetcode.com/problems/maximize-the-confusion-of-an-exam/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,7 @@
11971197
[2011][2011l]|[执行操作后的变量值][2011] |![py]&nbsp;&nbsp;![rs]
11981198
[2013][2013l]|[检测正方形][2013] |![rs]
11991199
[2016][2016l]|[增量元素之间的最大差值][2016] |![rs]
1200+
[2017][2017l]|[网格游戏][2017] |![rs]
12001201
[2022][2022l]|[将一维数组转变成二维数组][2022] |![rs]
12011202
[2023][2023l]|[连接后等于目标字符串的字符串对][2023] |![py]
12021203
[2024][2024l]|[考试的最大困扰度][2024] |![rs]
@@ -2700,6 +2701,7 @@
27002701
[2011]:Problemset/2011-Final%20Value%20of%20Variable%20After%20Performing%20Operations/README_CN.md#2011-执行操作后的变量值
27012702
[2013]:Problemset/2013-Detect%20Squares/README_CN.md#2013-检测正方形
27022703
[2016]:Problemset/2016-Maximum%20Difference%20Between%20Increasing%20Elements/README_CN.md#2016-增量元素之间的最大差值
2704+
[2017]:Problemset/2017-Grid%20Game/README_CN.md#2017-网格游戏
27032705
[2022]:Problemset/2022-Convert%201D%20Array%20Into%202D%20Array/README_CN.md#2022-将一维数组转变成二维数组
27042706
[2023]:Problemset/2023-Number%20of%20Pairs%20of%20Strings%20With%20Concatenation%20Equal%20to%20Target/README_CN.md#2023-连接后等于目标字符串的字符串对
27052707
[2024]:Problemset/2024-Maximize%20the%20Confusion%20of%20an%20Exam/README_CN.md#2024-考试的最大困扰度
@@ -4202,6 +4204,7 @@
42024204
[2011l]:https://leetcode.cn/problems/final-value-of-variable-after-performing-operations/
42034205
[2013l]:https://leetcode.cn/problems/detect-squares/
42044206
[2016l]:https://leetcode.cn/problems/maximum-difference-between-increasing-elements/
4207+
[2017l]:https://leetcode.cn/problems/grid-game/
42054208
[2022l]:https://leetcode.cn/problems/convert-1d-array-into-2d-array/
42064209
[2023l]:https://leetcode.cn/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/
42074210
[2024l]:https://leetcode.cn/problems/maximize-the-confusion-of-an-exam/

0 commit comments

Comments
 (0)