Minimize a Top-to-Bottom Path with Adjacent-Column Steps

Quick Overview

Minimize a Top-to-Bottom Path with Adjacent-Column Steps. A falling path in square matrix begins at any cell of the first row and chooses one cell in every subsequent row.

Minimize a Top-to-Bottom Path with Adjacent-Column Steps

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem A falling path in square `matrix` begins at any cell of the first row and chooses one cell in every subsequent row. From column c, the next row may use c-1,c,or c+1 when that column is inside the matrix. Return the minimum sum of a complete falling path. ```cpp class Solution { public: int minFallingPathSum(vector<vector<int>>& matrix); }; ``` ### Examples **Example 1:** ```text Input: matrix = [[3,8],[4,-2]] Output: 1 Explanation: Choose3, then diagonal-2, for total1; the other possible totals are7,12,and6. ``` **Example 2:** ```text Input: matrix = [[-7]] Output: -7 Explanation: The only path contains the single cell. ``` ### Constraints - `n == matrix.length == matrix[i].length`. - `1 <= n <= 100`. - `-100 <= matrix[i][j] <= 100`. ### Contract considerations The matrix is square, and every path contains exactly one cell per row. ```hint Read the boundary condition The best path to a cell depends only on three costs in the preceding row. ``` ### Boundary reminders The matrix is square, and every path contains exactly one cell per row.

Overview: Minimize a Top-to-Bottom Path with Adjacent-Column Steps. A falling path in square matrix begins at any cell of the first row and chooses one cell in every subsequent row.

Google logo
Google
Jun 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

A falling path in square matrix begins at any cell of the first row and chooses one cell in every subsequent row. From column c, the next row may use c-1,c,or c+1 when that column is inside the matrix. Return the minimum sum of a complete falling path.

class Solution {
public:
    int minFallingPathSum(vector<vector<int>>& matrix);
};

Examples

Example 1:

Input: matrix = [[3,8],[4,-2]]
Output: 1
Explanation: Choose3, then diagonal-2, for total1; the other possible totals are7,12,and6.

Example 2:

Input: matrix = [[-7]]
Output: -7
Explanation: The only path contains the single cell.

Constraints

  • n == matrix.length == matrix[i].length .
  • 1 <= n <= 100 .
  • -100 <= matrix[i][j] <= 100 .

Contract considerations

The matrix is square, and every path contains exactly one cell per row.

Boundary reminders

The matrix is square, and every path contains exactly one cell per row.