Package g2001_2100.s2017_grid_game
Class Solution
java.lang.Object
g2001_2100.s2017_grid_game.Solution
2017 - Grid Game.<p>Medium</p>
<p>You are given a <strong>0-indexed</strong> 2D array <code>grid</code> of size <code>2 x n</code>, where <code>grid[r][c]</code> represents the number of points at position <code>(r, c)</code> on the matrix. Two robots are playing a game on this matrix.</p>
<p>Both robots initially start at <code>(0, 0)</code> and want to reach <code>(1, n-1)</code>. Each robot may only move to the <strong>right</strong> (<code>(r, c)</code> to <code>(r, c + 1)</code>) or <strong>down</strong> (<code>(r, c)</code> to <code>(r + 1, c)</code>).</p>
<p>At the start of the game, the <strong>first</strong> robot moves from <code>(0, 0)</code> to <code>(1, n-1)</code>, collecting all the points from the cells on its path. For all cells <code>(r, c)</code> traversed on the path, <code>grid[r][c]</code> is set to <code>0</code>. Then, the <strong>second</strong> robot moves from <code>(0, 0)</code> to <code>(1, n-1)</code>, collecting the points on its path. Note that their paths may intersect with one another.</p>
<p>The <strong>first</strong> robot wants to <strong>minimize</strong> the number of points collected by the <strong>second</strong> robot. In contrast, the <strong>second</strong> robot wants to <strong>maximize</strong> the number of points it collects. If both robots play <strong>optimally</strong> , return <em>the <strong>number of points</strong> collected by the <strong>second</strong> robot.</em></p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/09/08/a1.png" alt="" /></p>
<p><strong>Input:</strong> grid = [[2,5,4],[1,5,1]]</p>
<p><strong>Output:</strong> 4</p>
<p><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.</p>
<p>The cells visited by the first robot are set to 0.</p>
<p>The second robot will collect 0 + 0 + 4 + 0 = 4 points.</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/09/08/a2.png" alt="" /></p>
<p><strong>Input:</strong> grid = [[3,3,1],[8,5,2]]</p>
<p><strong>Output:</strong> 4</p>
<p><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.</p>
<p>The cells visited by the first robot are set to 0.</p>
<p>The second robot will collect 0 + 3 + 1 + 0 = 4 points.</p>
<p><strong>Example 3:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/09/08/a3.png" alt="" /></p>
<p><strong>Input:</strong> grid = [[1,3,1,15],[1,3,3,1]]</p>
<p><strong>Output:</strong> 7</p>
<p><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.</p>
<p>The cells visited by the first robot are set to 0.</p>
<p>The second robot will collect 0 + 1 + 3 + 3 + 0 = 7 points.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>grid.length == 2</code></li>
<li><code>n == grid[r].length</code></li>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= grid[r][c] <= 10<sup>5</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
gridGame
public long gridGame(int[][] grid)
-