본문 바로가기
Programming/Algorithm

[leetcode] MinimumPathSum

by 읽고 쓰는 개발자 2020. 11. 29.

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

 

Minimum 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

public class MinimumPathSum {
    public static void main(String[] args) {
        System.out.println(minPathSum(new int[][] {
                                                    {1,3,1},
                                                      {1,5,1},
                                                      {4,2,1}
                                                                }));
    }

//    DP
    public static int minPathSum(int[][] grid) {
        int answer = 0;
        int n = grid.length;
        int m = grid[0].length;
        int[][] sumGrid = new int[n][m];

        for(int i = 0 ; i < n; i++) {
            for( int j = 0 ; j < m; j++) {
                int min = Integer.MAX_VALUE;
                if(i - 1 >= 0) {
                    min = sumGrid[i-1][j];
                }
                if(j - 1 >= 0) {
                    min = Math.min(min, sumGrid[i][j-1]);
                }
                if(i == 0 && j == 0 ) sumGrid[i][j] = grid[i][j];
                else sumGrid[i][j] = min + grid[i][j];
            }
        }

        return sumGrid[n-1][m-1];

    }
}

 

'Programming > Algorithm' 카테고리의 다른 글

[leetcode] String to Integer (atoi)  (0) 2020.11.29
[leetcode] MinStack  (0) 2020.11.29
[leetcode] MergeKLists  (0) 2020.11.29
[leetcode] MergeIntervals  (0) 2020.11.29
[leetcode] LRUCache  (0) 2020.11.29