-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2017. Grid Game #1192
Comments
We'll use the following approach:
Let's implement this solution in PHP: 2017. Grid Game <?php
function gridGame($grid) {
$n = count($grid[0]);
// Calculate prefix sums for both rows
$prefixTop = array_fill(0, $n + 1, 0);
$prefixBottom = array_fill(0, $n + 1, 0);
for ($i = 0; $i < $n; $i++) {
$prefixTop[$i + 1] = $prefixTop[$i] + $grid[0][$i];
$prefixBottom[$i + 1] = $prefixBottom[$i] + $grid[1][$i];
}
$minPointsForSecond = PHP_INT_MAX;
// Iterate over all possible transition points
for ($i = 0; $i < $n; $i++) {
// Points remaining in the top row after $i
$topRemaining = $prefixTop[$n] - $prefixTop[$i + 1];
// Points remaining in the bottom row before $i
$bottomRemaining = $prefixBottom[$i];
// Maximum points the second robot can collect
$secondRobotPoints = max($topRemaining, $bottomRemaining);
// Minimize the maximum points for the second robot
$minPointsForSecond = min($minPointsForSecond, $secondRobotPoints);
}
return $minPointsForSecond;
}
// Example usage
$grid1 = [[2, 5, 4], [1, 5, 1]];
$grid2 = [[3, 3, 1], [8, 5, 2]];
$grid3 = [[1, 3, 1, 15], [1, 3, 3, 1]];
echo gridGame($grid1) . "\n"; // Output: 4
echo gridGame($grid2) . "\n"; // Output: 4
echo gridGame($grid3) . "\n"; // Output: 7
?> Explanation:
Example WalkthroughInput:
|
Co-authored-by: kovatz <[email protected]> Co-authored-by: topugit <[email protected]> Co-authored-by: basharul-siddike <[email protected]> Co-authored-by: hafijul233 <[email protected]>
Discussed in #1191
Originally posted by mah-shamim January 21, 2025
Topics:
Array
,Matrix
,Prefix Sum
You are given a 0-indexed 2D array
grid
of size2 x n
, wheregrid[r][c]
represents the number of points at position(r, c)
on the matrix. Two robots are playing a game on this matrix.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)
).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 to0
. 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.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.
Example 1:
The cells visited by the first robot are set to 0.
The second robot will collect 0 + 0 + 4 + 0 = 4 points.
Example 2:
The cells visited by the first robot are set to 0.
The second robot will collect 0 + 3 + 1 + 0 = 4 points.
Example 3:
The cells visited by the first robot are set to 0.
The second robot will collect 0 + 1 + 3 + 3 + 0 = 7 points.
Constraints:
grid.length == 2
n == grid[r].length
1 <= n <= 5 * 104
1 <= grid[r][c] <= 105
Hint:
n
choices for when thefirst
robot moves to thesecond
row.prefix
sums to help solve this problem?The text was updated successfully, but these errors were encountered: