Skip to content

Commit d50040e

Browse files
committed
Sync LeetCode submission Runtime - 95 ms (63.61%), Memory - 29.7 MB (48.20%)
1 parent c222547 commit d50040e

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

2145-grid-game/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<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>
2+
3+
<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>
4+
5+
<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>
6+
7+
<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 <b>number of points</b> collected by the <strong>second</strong> robot.</em></p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
<img alt="" src="https://assets.leetcode.com/uploads/2021/09/08/a1.png" style="width: 388px; height: 103px;" />
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+
<p><strong class="example">Example 2:</strong></p>
21+
<img alt="" src="https://assets.leetcode.com/uploads/2021/09/08/a2.png" style="width: 384px; height: 105px;" />
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+
<p><strong class="example">Example 3:</strong></p>
31+
<img alt="" src="https://assets.leetcode.com/uploads/2021/09/08/a3.png" style="width: 493px; height: 103px;" />
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+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>grid.length == 2</code></li>
45+
<li><code>n == grid[r].length</code></li>
46+
<li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
47+
<li><code>1 &lt;= grid[r][c] &lt;= 10<sup>5</sup></code></li>
48+
</ul>

2145-grid-game/solution.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Approach 1: Prefix and Suffix Sum
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def gridGame(self, grid: List[List[int]]) -> int:
8+
first_row_sum = sum(grid[0])
9+
second_row_sum = 0
10+
minimum_sum = float('inf')
11+
12+
for turn_index in range(len(grid[0])):
13+
first_row_sum -= grid[0][turn_index]
14+
# Find the minimum maximum value out of first_row_sum and second_row_sum.
15+
minimum_sum = min(minimum_sum, max(first_row_sum, second_row_sum))
16+
second_row_sum += grid[1][turn_index]
17+
18+
return minimum_sum
19+
20+

0 commit comments

Comments
 (0)