-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc_64.cpp
More file actions
43 lines (40 loc) · 702 Bytes
/
Copy pathlc_64.cpp
File metadata and controls
43 lines (40 loc) · 702 Bytes
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
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size();
if( m < 1)
return 0;
int n = grid[0].size();
if( n == 0)
return 0;
int **dp = new int*[m+1];
int i = 0;
for(; i <= m; ++i)
{
dp[i] = new int[n+1];
}
int j = 0;
dp[1][0] = 0;
for( i = 1; i <= n; ++i )
{
dp[1][i] = dp[1][i-1] + grid[0][i-1];
}
dp[0][1] = 0;
for( j = 1; j <= m; ++j )
{
dp[j][1] = dp[j-1][1] + grid[j-1][0];
}
for( i = 2; i <= m; ++i)
{
for( j = 2; j <= n; ++j)
{
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i-1][j-1];
}
}
return dp[m][n];
}
};
/**
*using O(n)
*space O(1) ?
*/