Skip to content

Commit

Permalink
added Minimum Path Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Nov 21, 2014
1 parent f6f92c6 commit 347dd47
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 38. Minimum Path Sum/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "solution.h"
#include <iostream>

int main()
{
std::vector<std::vector<int> > vec{{0,2,1,5,2,6,9},{3,5,4,7,4,3,8},{6,9,3,8,5,7,0}};
Solution s;
std::cout << s.minPathSum(vec) << std::endl;
return 0;
}
17 changes: 17 additions & 0 deletions 38. Minimum Path Sum/solution.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <algorithm>
#include <vector>

using std::vector;

class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
for (decltype(grid.size()) i=0; i<grid.size(); ++i)
for (decltype(grid[0].size()) j=0; j<grid[0].size(); ++j)
if (i == 0 && j == 0) continue;
else if (i == 0) grid[i][j] += grid[i][j-1];
else if (j == 0) grid[i][j] += grid[i-1][j];
else grid[i][j] += std::min(grid[i-1][j], grid[i][j-1]);
return grid.back().back();
}
};

0 comments on commit 347dd47

Please sign in to comment.