Background个
我正在努力解决以下leetcode problem个问题.我已经编写了下面的函数,它似乎通过了示例测试用例.
My Code个
def gridGame(grid):
robot_score = 0
for _ in range(2):
robot_score = 0
row_one_sum = sum(grid[0])
row_two_sum = sum(grid[1])
row = 0
col = 0
while row < len(grid) and col < len(grid[0]):
# get current points
robot_score += grid[row][col]
# subtract points from sum
if row == 0:
row_one_sum -= grid[0][col]
else:
row_two_sum -= grid[1][col]
# set current grid to 0
grid[row][col] = 0
# figure out next move
if row == 0:
if row_two_sum > row_one_sum or col == len(grid[0]) - 1:
row += 1
else:
row_two_sum -= grid[row - 1][col - 1]
col += 1
else:
col += 1
return robot_score
Test cases i am passing个
grid = [[1,3,1,15],[1,3,3,1]]
grid = [[3,3,1],[8,5,2]]
grid = [[2,5,4],[1,5,1]]
Test Case I am Failing个
gridGame([[10, 12, 14, 19, 19, 12, 19, 2, 17], [20, 7, 17, 14, 3, 1, 1, 17, 12]])
我试着调试了2.5个小时,但没有运气,我知道我可以在网上找到解决方案,但我正在努力理解我做错了什么.
我的总体方法是,我需要得到每一行的总和,然后我知道如果我移动正确,我可以得到多少分,而如果我向下移动,我可以得到多少分.