배열크기 그대로 dp테이블 생성하고
첫번쨰줄은 자기값그대로,두번째줄부터는 자기가 선택할수있는 상위중에서 가장작은값+자기값 채워넣고 마지막줄에서 min값 리턴
정답
class Solution {
fun minFallingPathSum(matrix: Array<IntArray>): Int {
val dpList = mutableListOf<MutableList<Int>>()
for (i in matrix){
dpList.add(MutableList(i.size){0})
}
for (i in 0 until matrix[0].size){
dpList[0][i]=matrix[0][i]
}
for (row in 1 until matrix.size){
for (col in 0 until matrix[row].size){
val tempList = mutableListOf<Int>()
if (col-1>=0){
tempList.add(dpList[row-1][col-1])
}
tempList.add(dpList[row-1][col])
if (col+1<dpList[row].size){
tempList.add(dpList[row-1][col+1])
}
dpList[row][col]=tempList.min()+matrix[row][col]
}
}
return dpList.last().min()
}
}
'알고리즘' 카테고리의 다른 글
릿코드 645. Set Mismatch 코틀린 (0) | 2024.01.22 |
---|---|
코틀린 198. House Robber 릿코드 (0) | 2024.01.21 |
릿코드 70. Climbing Stairs 코틀린 (0) | 2024.01.18 |
릿코드 1207. Unique Number of Occurrences 코틀린 (0) | 2024.01.17 |
릿코드 380. Insert Delete GetRandom O(1) 코틀린 (0) | 2024.01.16 |