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

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

 

Minimum Falling Path Sum II - 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[][]} arr
 * @return {number}
 */
var minFallingPathSum = function(arr) {
    // ------------------------------    
    // DP를 구하기 위한 어레이 설정
    // ------------------------------    
    const dp        = arr.slice()
    const rowSize   = arr[0].length;
        
    // ------------------------------
    // Falling path 검색
    // ------------------------------
    var res = 65535;
    for( let i=1; i<arr.length; ++i )
    {        
        for( let j=0; j<rowSize; ++j )
        {            
            let min = 65535;
            for( let k=0; k<rowSize; ++k )
            {
                if( j == k )    continue;
                min = min < arr[i-1][k] ? min : arr[i-1][k];
            }
            dp[i][j] = min + arr[i][j];
            
            if( i == rowSize-1 )
                res = res<dp[i][j] ? res : dp[i][j];
        }        
    }    
    return res;
};
posted by Sense.J
: