Problem · Array

Minimum Path Sum

Learn this problem
MediumGoldman Sachs logoGoldman SachsFULLTIMEPHONE SCREEN

Problem statement

Given a nonempty rectangular matrix grid of nonnegative integers, start at the top-left cell and reach the bottom-right cell.

At each step, move exactly one cell right or one cell down. Return the minimum possible sum of the values on the path, including both endpoints.

Function

minimumPathSum(grid: int[][]) → int

Examples

Example 1

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

The path 1, 3, 1, 1, 1 has sum 7.

Example 2

grid = [[1,2,3],[4,5,6]]return = 12

Moving right, right, then down gives 1 + 2 + 3 + 6.

Constraints

  • 1 <= grid.length, grid[0].length <= 500
  • Every row has the same length.
  • 0 <= grid[r][c] <= 10000
  • The answer fits in a signed 32-bit integer.

More Goldman Sachs problems

drafts saved locally
public int minimumPathSum(int[][] grid) {
  // write your code here
}
grid[[1,3,1],[1,5,1],[4,2,1]]
expected7
checking account