Skip to content

Commit c0cfdb7

Browse files
committed
2017. Grid Game: RETRY
1 parent 717a017 commit c0cfdb7

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,3 +1520,4 @@ mod s2012_sum_of_beauty_in_the_array;
15201520
mod s2013_detect_squares;
15211521
mod s2014_longest_subsequence_repeated_k_times;
15221522
mod s2016_maximum_difference_between_increasing_elements;
1523+
mod s2017_grid_game;

src/solution/s2017_grid_game.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* [2017] Grid Game
3+
*
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+
* 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)).
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+
* 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.
8+
*
9+
* Example 1:
10+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/09/08/a1.png" style="width: 388px; height: 103px;" />
11+
* Input: grid = [[2,5,4],[1,5,1]]
12+
* Output: 4
13+
* 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.
14+
* The cells visited by the first robot are set to 0.
15+
* The second robot will collect 0 + 0 + 4 + 0 = 4 points.
16+
*
17+
* Example 2:
18+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/09/08/a2.png" style="width: 384px; height: 105px;" />
19+
* Input: grid = [[3,3,1],[8,5,2]]
20+
* Output: 4
21+
* 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.
22+
* The cells visited by the first robot are set to 0.
23+
* The second robot will collect 0 + 3 + 1 + 0 = 4 points.
24+
*
25+
* Example 3:
26+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/09/08/a3.png" style="width: 493px; height: 103px;" />
27+
* Input: grid = [[1,3,1,15],[1,3,3,1]]
28+
* Output: 7
29+
* 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.
30+
* The cells visited by the first robot are set to 0.
31+
* The second robot will collect 0 + 1 + 3 + 3 + 0 = 7 points.
32+
*
33+
*
34+
* Constraints:
35+
*
36+
* grid.length == 2
37+
* n == grid[r].length
38+
* 1 <= n <= 5 * 10^4
39+
* 1 <= grid[r][c] <= 10^5
40+
*
41+
*/
42+
pub struct Solution {}
43+
44+
// problem: https://leetcode.com/problems/grid-game/
45+
// discuss: https://leetcode.com/problems/grid-game/discuss/?currentPage=1&orderBy=most_votes&query=
46+
47+
// submission codes start here
48+
49+
impl Solution {
50+
pub fn grid_game(grid: Vec<Vec<i32>>) -> i64 {
51+
0
52+
}
53+
}
54+
55+
// submission codes end
56+
57+
#[cfg(test)]
58+
mod tests {
59+
use super::*;
60+
61+
#[test]
62+
#[ignore]
63+
fn test_2017_example_1() {
64+
let grid = vec![vec![2, 5, 4], vec![1, 5, 1]];
65+
66+
let result = 4;
67+
68+
assert_eq!(Solution::grid_game(grid), result);
69+
}
70+
71+
#[test]
72+
#[ignore]
73+
fn test_2017_example_2() {
74+
let grid = vec![vec![3, 3, 1], vec![8, 5, 2]];
75+
76+
let result = 4;
77+
78+
assert_eq!(Solution::grid_game(grid), result);
79+
}
80+
81+
#[test]
82+
#[ignore]
83+
fn test_2017_example_3() {
84+
let grid = vec![vec![3, 3, 1], vec![8, 5, 2]];
85+
86+
let result = 4;
87+
88+
assert_eq!(Solution::grid_game(grid), result);
89+
}
90+
}

0 commit comments

Comments
 (0)