The trick here is to name the state correctly, then let the implementation follow. For 1289. Minimum Falling Path Sum II, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are minFallingPathSum, last_row.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 32 ms, faster than 91.25% of C++ online submissions for Minimum Falling Path Sum II.
02//Memory Usage: 13.1 MB, less than 100.00% of C++ online submissions for Minimum Falling Path Sum II.
03class Solution {
04public:
05 int minFallingPathSum(vector<vector<int>>& arr) {
06 int m = arr.size(), n = arr[0].size();
07 int ans = INT_MAX;
08 vector<vector<int>> dp(m, vector<int>(n, 0));
09
10 //initialize dp's first row
11 for(int j = 0; j < n; j++){
12 dp[0][j] = arr[0][j];
13 }
14
15 for(int i = 1; i < m; i++){
16 vector<int> last_row(dp[i-1].begin(), dp[i-1].end());
17 // for(int e : last_row) cout << e << " ";
18 // cout << endl;
19 auto last_row_min_it = min_element(last_row.begin(), last_row.end());
20 int last_row_min_idx = last_row_min_it - last_row.begin();
21 // cout << "min at " << last_row_min_idx << endl;
22 int last_row_min = *last_row_min_it;
23 nth_element(last_row.begin(), last_row.begin()+1, last_row.end(), less<int>());
24 int last_row_2min = last_row[1];
25 // cout << "min: " << last_row_min << ", 2nd min: " << last_row_2min << endl;
26 for(int j = 0; j < n; j++){
27 if(j == last_row_min_idx){
28 dp[i][j] = arr[i][j] + last_row_2min;
29 }else{
30 dp[i][j] = arr[i][j] + last_row_min;
31 }
32 }
33 }
34 // for(int e : dp[m-1]) cout << e << " ";
35 // cout << endl;
36
37 return *min_element(dp[m-1].begin(), dp[m-1].end());
38 }
39};
Cost