Problem · Array

Maximum-Value Grid Path

Learn this problem
MediumGoldman Sachs logoGoldman SachsFULLTIMEPHONE SCREEN

Problem statement

Given a nonempty rectangular integer matrix grid, start at its top-left cell and reach its bottom-right cell by moving only one cell right or one cell down.

Collect the value of every visited cell, including both endpoints. Return the maximum total value of a valid path.

Function

maximumGridPathValue(grid: int[][]) → long

Examples

Example 1

grid = [[5,1,2],[4,0,3],[2,7,1]]return = 19

The path 5,4,2,7,1 has total 19.

Example 2

grid = [[-1,-2],[-3,-4]]return = -7

Moving right then down gives -1-2-4=-7.

Constraints

  • 1 <= grid.length, grid[i].length <= 1000
  • All rows have the same length.
  • -10^9 <= grid[i][j] <= 10^9
  • The answer fits in a signed 64-bit integer.

More Goldman Sachs problems

drafts saved locally
public long maximumGridPathValue(int[][] grid) {
  // write your code here
}
grid[[5,1,2],[4,0,3],[2,7,1]]
expected19
checking account