Skip to content

Commit c27f16e

Browse files
committed
1289 solved.
1 parent cf55187 commit c27f16e

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.25)
2+
project(cpp_1289)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(cpp_1289 main.cpp)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// Source : https://leetcode.com/problems/minimum-falling-path-sum-ii/
2+
/// Author : liuyubobobo
3+
/// Time : 2023-08-09
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Memoization
12+
/// Time Complexity: O(n^3)
13+
/// Space Complexity: O(n^2)
14+
class Solution {
15+
public:
16+
int minFallingPathSum(vector<vector<int>>& grid) {
17+
18+
int n = grid.size();
19+
20+
vector<vector<int>> dp(n, vector<int>(n + 1, -1));
21+
return dfs(n, grid, 0, n, dp);
22+
}
23+
24+
private:
25+
int dfs(int n, const vector<vector<int>>& grid, int row, int last_c, vector<vector<int>>& dp) {
26+
27+
if (row == n) return 0;
28+
if (dp[row][last_c] != -1) return dp[row][last_c];
29+
30+
int res = INT_MAX;
31+
for (int j = 0; j < n; j ++) {
32+
if (j == last_c) continue;
33+
res = min(res, grid[row][j] + dfs(n, grid, row + 1, j, dp));
34+
}
35+
return dp[row][last_c] = res;
36+
}
37+
};
38+
39+
40+
int main() {
41+
42+
return 0;
43+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
12561256
| 1286 | [Iterator for Combination](https://leetcode.com/problems/iterator-for-combination/) | [solution](https://leetcode.com/problems/iterator-for-combination/solution/) | [C++](1001-1500/1286-Iterator-for-Combination/cpp-1286/) | | |
12571257
| | | | | | |
12581258
| 1288 | [Remove Covered Intervals](https://leetcode.com/problems/remove-covered-intervals/) | [solution](https://leetcode.com/problems/remove-covered-intervals/solution/) | [C++](1001-1500/1288-Remove-Covered-Intervals/cpp-1288/) | | |
1259-
| | | | | | |
1259+
| 1289 | [Minimum Falling Path Sum II](https://leetcode.com/problems/minimum-falling-path-sum-ii/) | [] | [C++](1001-1500/1289-Minimum-Falling-Path-Sum-II/cpp-1289/) | | |
12601260
| 1290 | [Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/) | [solution](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/solution/) | [C++](1001-1500/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/cpp-1290/) | | |
12611261
| 1291 | [Sequential Digits](https://leetcode.com/problems/sequential-digits/) | [solution](https://leetcode.com/problems/sequential-digits/solution/) | [C++](1001-1500/1291-Sequential-Digits/cpp-1291/) | | |
12621262
| | | | | | |

0 commit comments

Comments
 (0)