Skip to content

Instantly share code, notes, and snippets.

@vectorijk
Created October 30, 2014 06:58
Show Gist options
  • Save vectorijk/01899e2991f1a440f012 to your computer and use it in GitHub Desktop.
Save vectorijk/01899e2991f1a440f012 to your computer and use it in GitHub Desktop.
Leetcode
//============================================================================
// Name : minimum-path-sum.cpp
// Author : jiangkai
// Version :
// Copyright :
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int minPathSum(vector<vector<int> > &grid) {
int n = grid.size();
if (n == 0 ) return 0;
int m = grid[0].size();
vector<vector<int> >dp(n+1, vector<int>(m+1, 0x3f3f3f3f));
dp[0][1] = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
dp[i][j] = grid[i-1][j-1] + min(dp[i][j-1], dp[i-1][j]);
}
}
return dp[n][m];
}
int main() {
std::ios_base::sync_with_stdio(0);
//freopen("in","r",stdin);
vector<vector<int> > grid;
int n,m,tmp;
vector<int> TmpV;
cin >> n >> m;
for(int i = 0; i < n; i++){
TmpV.clear();
for(int j = 0; j < m; j++ ){
cin >> tmp;
TmpV.push_back(tmp);
}
grid.push_back(TmpV);
}
cout << minPathSum(grid);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment