Home LeetCode - 64. Minimum Path Sum
Post
Cancel

LeetCode - 64. Minimum Path Sum

64. Minimum Path Sum - medium

문제

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

제한사항

입출력 예

1
2
3
4
5
6
7
8
Input:
[
  [1,3,1],
  [1,5,1],
  [4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.

풀이

  • DP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();

        for(auto i = 0 ; i < m ; ++i){
            for(auto j = 0 ; j < n ; ++j){
                if(i == 0 && j == 0)
                    continue;
                    
                // row, colum의 첫줄의 경로는 한가지 밖에 없음
                if(i == 0 && j != 0){
                    grid[i][j] += grid[i][j-1];    
                }
                else if(i !=0 && j == 0){
                    grid[i][j] += grid[i-1][j];                        
                }

                // 경로는 위 또는 왼쪽에서 밖에 올수 없으므로,
                // 둘중 작은 값을 선택
                else{
                    grid[i][j] += min(grid[i][j-1], grid[i-1][j]);
                }
            }
        }
        
        return grid[m-1][n-1];
    }
};
This post is licensed under CC BY 4.0 by the author.