Skip to content

Commit 677ec50

Browse files
committed
210125
1 parent 215c479 commit 677ec50

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

leetcode/editor/en/Q2017/GridGame.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List
2+
3+
4+
# leetcode submit region begin(Prohibit modification and deletion)
5+
class Solution:
6+
def gridGame(self, grid: List[List[int]]) -> int:
7+
N = len(grid[0])
8+
INF = 10 ** 20
9+
10+
presum_1 = [0] * N
11+
presum_2 = [0] * N
12+
presum_1[0] = grid[0][0]
13+
presum_2[0] = grid[1][0]
14+
for i in range(1, N):
15+
presum_1[i] = presum_1[i - 1] + grid[0][i]
16+
presum_2[i] = presum_2[i - 1] + grid[1][i]
17+
best = INF
18+
for j in range(N):
19+
# if i go down here:
20+
ans1 = presum_1[j] + (presum_2[-1] - (presum_2[j - 1] if j > 0 else 0))
21+
ans2 = max((presum_2[j - 1] if j > 0 else 0), presum_1[-1] - presum_1[j])
22+
best = min(best, ans2)
23+
return best
24+
25+
26+
# leetcode submit region end(Prohibit modification and deletion)
27+
28+
29+
class GridGame(Solution):
30+
pass

main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from leetcode.editor.en.Q1368.MinimumCostToMakeAtLeastOneValidPathInAGrid import \
2-
MinimumCostToMakeAtLeastOneValidPathInAGrid
1+
from leetcode.editor.en.Q2017.GridGame import GridGame
32

43
if __name__ == '__main__':
54
print(
6-
MinimumCostToMakeAtLeastOneValidPathInAGrid().minCost([[1, 1, 1, 1], [2, 2, 2, 2], [1, 1, 1, 1], [2, 2, 2, 2]]))
5+
GridGame().gridGame([[20, 3, 20, 17, 2, 12, 15, 17, 4, 15], [20, 10, 13, 14, 15, 5, 2, 3, 14, 3]]))

0 commit comments

Comments
 (0)