Skip to content

Commit

Permalink
leetcode, algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
zotvent authored Apr 9, 2024
1 parent c091152 commit e29ecd8
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions leetcode/algorithms/931 - Minimum Falling Path Sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
int minFallingPathSum(vector<vector<int>>& matrix) {
const int rows = matrix.size();
const int cols = matrix[0].size();
vector<vector<int>> dp(rows, vector<int>(cols, INT_MAX));
dp[0] = matrix[0];
for (int i = 1; i < rows; i++) {
for (int j = 0; j < cols; j++) {
dp[i][j] = min(dp[i][j], dp[i - 1][j]);
if (j > 0) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
if (j < cols - 1) dp[i][j] = min(dp[i][j], dp[i - 1][j + 1]);
dp[i][j] += matrix[i][j];
}
}
int res = INT_MAX;
for (int i = 0; i < cols; i++) {
res = min(res, dp[rows - 1][i]);
}
return res;
}
};

0 comments on commit e29ecd8

Please sign in to comment.