알고리즘 공부 2020. 8. 20. 22:28

https://leetcode.com/problems/minimum-falling-path-sum/

 

Minimum Falling Path Sum - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

/**
 * @param {number[][]} A
 * @return {number}
 */
var minFallingPathSum = function(A) {    
    // ------------------------------    
    // DP를 구하기 위한 어레이 설정
    // ------------------------------
    const rowsize = A.length +2;
    const dp= Array( rowsize );    
    dp[0]   = Array( rowsize ).fill(101);    
    
    for( let i=0; i<A.length; ++i )
    {
        dp[i+1] = Array( rowsize ).fill(101);    
        for( let j=0; j<A[0].length; ++j )        
            dp[i+1][j+1] = A[i][j];        
    }
    // ------------------------------
    // 끝
    // ------------------------------        
    
    // ------------------------------
    // Falling path 검색
    // ------------------------------
    for( let i=1; i<A.length; ++i )
    {
        for( let j=0; j<A[0].length; ++j )        
            dp[i+1][j+1] = Math.min( dp[i][j], dp[i][j+1], dp[i][j+2]) + A[i][j];        
    }
    // ------------------------------   
    
    var res = 101;
    for( let j=0; j<dp[0].length; ++j )            
        res = Math.min( res, dp[A.length][j] )    
    
    return res;
};
posted by Sense.J
: