Maximum Grid Path Score
Problem statement
Given a rectangular integer grid, find the largest score of a path from its top-left cell to its bottom-right cell. At each step move exactly one cell right or down. The path includes both endpoints and must reach the destination. Its score is the sum of the values of all visited cells.
Return the maximum score as a 64-bit integer. If there are no rows or no columns, return zero. Negative scores are allowed; you may not stop early or choose an empty route for a nonempty grid.
Function
maximumGridPathScore(grid: int[][]) → longExamples
Example 1
grid = [[1,2,3],[4,5,6]]return = 16The best route visits 1,4,5,6: down, right, right. Its sum is 16. The other two routes sum to 12 and 14.
Example 2
grid = [[-1,-2],[-3,-4]]return = -7The two complete routes score -1-2-4=-7 and -1-3-4=-8. Choose -7. A route must still reach the destination, so returning zero is invalid.
Example 3
grid = []return = 0There are no cells and no route, so the defined empty-grid result is zero.
Constraints
0 <= grid.length <= 50- Every row contains the same number of entries, between
0and50, inclusive. -1000000000 <= grid[r][c] <= 1000000000