Skip to content

Commit

Permalink
more clean implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Apr 2, 2019
1 parent d0b2896 commit d3946ff
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions algorithms/cpp/minimumPathSum/minimumPathSum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,18 @@
#include <vector>
using namespace std;

int minPathSum(vector<vector<int> > &grid) {
if (grid.size()<=0){
return 0;
}
int i, j;
for(i=0; i<grid.size(); i++){
for(j=0; j<grid[i].size(); j++){
int top = i-1<0 ? INT_MAX : grid[i-1][j] ;
int left = j-1<0 ? INT_MAX : grid[i][j-1];
if (top==INT_MAX && left==INT_MAX){
continue;
}
grid[i][j] += (top < left? top: left);

int minPathSum(vector<vector<int>>& grid) {
for (int i=0; i<grid.size(); i++) {
for (int j=0; j<grid[0].size(); j++) {
if (i==0 && j==0) continue;
else if (i==0) grid[0][j] += grid[0][j-1];
else if (j==0) grid[i][0] += grid[i-1][j];
else grid[i][j] += min( grid[i-1][j], grid[i][j-1]);
}
}

return grid[grid.size()-1][grid[0].size()-1];
}


int main()
{
int a[6][2]={{7,2},{6,6},{8,6},{8,7},{5,0},{6,0}};
Expand All @@ -59,7 +50,7 @@ int main()
}
grid.push_back(v);
}

cout << "minPathSum=" << minPathSum(grid) << endl;

return 0;
Expand Down

0 comments on commit d3946ff

Please sign in to comment.