You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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.
4
+
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
+
7
+
// 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.
8
+
9
+
// 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.
10
+
11
+
// Example 1:
12
+
13
+
// Input: grid = [[2,5,4],[1,5,1]]
14
+
// Output: 4
15
+
// 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.
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
+
// Example 2:
19
+
20
+
// Input: grid = [[3,3,1],[8,5,2]]
21
+
// Output: 4
22
+
// 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.
23
+
// The cells visited by the first robot are set to 0.
24
+
// The second robot will collect 0 + 3 + 1 + 0 = 4 points.
25
+
// Example 3:
26
+
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
+
// Constraints:
34
+
35
+
// grid.length == 2
36
+
// n == grid[r].length
37
+
// 1 <= n <= 5 * 104
38
+
// 1 <= grid[r][c] <= 105
39
+
40
+
classSolution
41
+
{
42
+
public:
43
+
longlonggridGame(vector<vector<int>> &grid)
44
+
{
45
+
longlong min_result = LLONG_MAX; // Start with a large number for the minimum score.
46
+
longlong row1_sum = accumulate(grid[0].begin(), grid[0].end(), 0LL); // Top row sum
47
+
longlong row2_sum = 0; // Bottom row sum starts at 0.
48
+
49
+
for (int i = 0; i < grid[0].size(); ++i)
50
+
{
51
+
row1_sum -= grid[0][i]; // Move the top player forward, subtract current value.
52
+
min_result = min(min_result, max(row1_sum, row2_sum)); // Update the minimum of the max scores.
53
+
row2_sum += grid[1][i]; // Move the bottom player forward, add current value.
54
+
}
55
+
56
+
return min_result; // Return the minimum maximum score.
57
+
}
58
+
};
59
+
60
+
/*
61
+
This code solves the Grid Game problem using a greedy approach:
62
+
1. Initialize min_result with maximum possible value to track minimum score
63
+
2. Calculate initial sum of top row (row1_sum)
64
+
3. Initialize bottom row sum (row2_sum) as 0
65
+
4. For each column in the grid:
66
+
- Subtract current top cell value from row1_sum (first robot's path)
67
+
- Compare remaining top sum (row1_sum) with accumulated bottom sum (row2_sum)
68
+
- Take maximum of these two as potential second robot's score
69
+
- Update min_result if current maximum is smaller
70
+
- Add current bottom cell value to row2_sum
71
+
5. Return min_result as optimal score for second robot
72
+
The algorithm minimizes the maximum possible score second robot can achieve
0 commit comments