Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 535ecf9

Browse files
Time: 3 ms (32.55%), Space: 15 MB (38.88%) - LeetHub
1 parent a2ff878 commit 535ecf9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int minFallingPathSum(vector<vector<int>>& matrix) {
4+
int n = matrix.size();
5+
int m = matrix[0].size();
6+
vector<vector<int>> dp(n, vector<int>(m, 1e9));
7+
for (int j = 0; j < m; j++) {
8+
dp[0][j] = matrix[0][j];
9+
}
10+
for (int i = 1; i < n; i++) {
11+
for (int j = 0; j < m; j++) {
12+
if (j - 1 >= 0) {
13+
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + matrix[i][j]);
14+
}
15+
if (j + 1 < m) {
16+
dp[i][j] = min(dp[i][j], dp[i - 1][j + 1] + matrix[i][j]);
17+
}
18+
dp[i][j] = min(dp[i][j], dp[i - 1][j] + matrix[i][j]);
19+
}
20+
}
21+
int ans = 1e9;
22+
for (int j = 0; j < m; j++) {
23+
ans = min(ans, dp[n - 1][j]);
24+
}
25+
return ans;
26+
}
27+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /