Posted on

Description

Submission

class Solution {
    int dp[201][201];
public:
    int minFallingPathSum(vector<vector<int>>& arr) {
        int n = arr.size();

        for(int i = 0; i < n; ++i) {
            dp[0][i] = arr[0][i];
        }

        for(int i = 1; i < n; ++i) {
            for(int j = 0; j < n; ++j) {
                int m = INT_MAX;
                for(int k = 0; k < n; ++k) {
                    if(k == j) continue;
                    m = min(m, dp[i-1][k]);
                }
                dp[i][j] = m + arr[i][j];
            }
        }
        
        int ret = INT_MAX;
        for(int i = 0; i < n; ++i) {
            ret = min(dp[n-1][i], ret);
        }
        
        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *