-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2017-grid-game.py
More file actions
23 lines (19 loc) · 675 Bytes
/
2017-grid-game.py
File metadata and controls
23 lines (19 loc) · 675 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# time complexity: O(n)
# space complexity: O(1)
from typing import List
class Solution:
def gridGame(self, grid: List[List[int]]) -> int:
firstRowSum = sum(grid[0])
secondRowSum = 0
minimumSum = float("inf")
for turnIndex in range(len(grid[0])):
firstRowSum -= grid[0][turnIndex]
minimumSum = min(minimumSum, max(firstRowSum, secondRowSum))
secondRowSum += grid[1][turnIndex]
return minimumSum
grid = [[2, 5, 4], [1, 5, 1]]
print(Solution().gridGame(grid))
grid = [[3, 3, 1], [8, 5, 2]]
print(Solution().gridGame(grid))
grid = [[1, 3, 1, 15], [1, 3, 3, 1]]
print(Solution().gridGame(grid))