Leetcode 931: Minimum Falling Path Sum

grid47
grid47
Exploring patterns and algorithms
Aug 5, 2024 5 min read

Given a square matrix of integers, your task is to find the minimum sum of any falling path through the matrix. A falling path starts at any element in the first row and chooses the next element from the row directly below it, which can be either directly below, diagonally left, or diagonally right.
Problem
Approach
Steps
Complexity
Input: You are given an n x n matrix, where each element represents a value. You need to find the falling path with the minimum sum.
Example: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Constraints:
• n == matrix.length == matrix[i].length
• 1 <= n <= 100
• -100 <= matrix[i][j] <= 100
Output: Return the minimum sum of a falling path.
Example: Output: 13
Constraints:
• The matrix will always be a square.
Goal: To find the falling path with the minimum sum, update the matrix values row by row, adding the smallest possible sum from the previous row to the current row.
Steps:
• 1. Start from the second row and update each element by adding the minimum of the three possible elements from the previous row (directly above, diagonally left, diagonally right).
• 2. Once the matrix is updated, the minimum sum will be the smallest value in the last row.
Goal: The input matrix will always be a square with integer values.
Steps:
• 1 <= n <= 100
• -100 <= matrix[i][j] <= 100
Assumptions:
• The matrix is square, and all elements are integers.
Input: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Explanation: To get the minimum falling path sum, start with any element in the first row, and for each subsequent row, pick the minimum value from the adjacent elements in the previous row. For this example, the minimum sum is 13, which is the path [1, 5, 7].

Input: Input: matrix = [[-19,57],[-40,-5]]
Explanation: For this example, the minimum falling path sum is -59, which follows the path [-19, -40].

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus