FastPrepMinimum Falling Path Sum

Minimum Falling Path Sum

Scale AI logoScale AI● MediumNEW GRADOA
Learn

Problem statement

Given a rectangular integer matrix, choose any cell in the top row. On each next row, move down-left, straight down, or down-right.

Return the minimum sum of a path that reaches the final row.

Function

minimumFallingPathSum(matrix: int[][]) → long

Examples

Example 1

matrix = [[2,1,3],[6,5,4],[7,8,9]]return = 13

The path 1 → 5 → 7 has sum 13.

Example 2

matrix = [[-19,57],[-40,-5]]return = -59

Choose -19, then move down to -40.

Example 3

matrix = [[5]]return = 5

The only cell is both the top and final row.

Constraints

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

More Scale AI problems

See Scale AI hiring insights
public long minimumFallingPathSum(int[][] matrix) {
    // write your code here
}
matrix[[2,1,3],[6,5,4],[7,8,9]]
expected13
Checking account…