Beat 100% CPP solutions (12ms)


  • 0
    L
       class Solution {
        public:
            int minPathSum(vector<vector<int>>& grid) {
                int ret;
                if ((grid.size()==0)||(grid[0].size()==0)) return ret;
                int m = grid.size();
                int n = grid[0].size();
                vector<vector<int>> dist(m,vector<int>(n,0));
                dist[0][0]=grid[0][0];
                for (int i=1;i<m;i++){
                    dist[i][0]=dist[i-1][0]+grid[i][0];
                }
                for (int j=1;j<n;j++){
                    dist[0][j]=dist[0][j-1]+grid[0][j];
                }
                if (m==1) return dist[0][n-1];
                if (n==1) return dist[m-1][0];
                for (int i=1;i<m;i++){
                    for(int j=1;j<n;j++){
                        dist[i][j]=min(dist[i-1][j],dist[i][j-1])+grid[i][j];
                    }
                }
                return dist[m-1][n-1];
                
            }
        
        };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.