# int32bit / leetcode

Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
in.txt
solve.cpp

## Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

## Solution

`dp[i][j]`表示`a[0][0]``a[i][j]`的最小路径, 则显然有动态方程`dp[i][j] = MIN(dp[i - 1][j], dp[i][j - 1])`,

```int minPathSum(vector<vector<int>> &grid) {
if (grid.size() < 1)
return 0;
int n = grid.size();
int m = grid[0].size();
vector<vector<int>> dp(n, vector<int>(m, 0));
dp[0][0] = grid[0][0];
for (int i = 1; i < n; ++i)
dp[i][0] = dp[i - 1][0] + grid[i][0];
for (int j = 1; j < m; ++j)
dp[0][j] = dp[0][j - 1] + grid[0][j];
for (int i = 1; i < n; ++i)
for (int j = 1; j < m; ++j)
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
return dp[n - 1][m - 1];
}```

```int minPathSum(vector<vector<int>> &grid) {
if (grid.size() < 1)
return 0;
int n = grid.size();
int m = grid[0].size();
vector<int> dp(n, 0);
dp[0] = grid[0][0];
for (int i = 1; i < n; ++i)
dp[i] = dp[i - 1] + grid[i][0];
for (int i = 1; i < n; ++i)
for (int j = 1; j < n; ++j) {
dp[j] = min(dp[j], dp[j - 1]) + grid[i][j];
}
return dp[n - 1];
}```
You can’t perform that action at this time.