您的位置:首页 > 其它

LeetCode - Minimum Path Sum

2013-12-21 23:34 429 查看
Minimum Path Sum

2013.12.21 23:32

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.

Solution1:

  Given a 2-d matrix of size m x n, with all elements non-negative, find a down-right path that adds up to a minimal sum. This problem can be solved using dynamic programming.

  Recurrence relation is sum[x][y] = min(sum[x - 1][y], sum[x][y - 1]) + a[x][y], where sum[x][y] is the minimum path sum of position (x, y), and a[x][y] is the element at position (x, y).

  Time complexity is O(m * n), space complexity is O(m * n).

Accepted code:

// 1AC
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int m, n;

m = grid.size();
if(m <= 0){
return 0;
}

n = grid[0].size();
if(n <= 0){
return 0;
}

int **arr = new int*[m];
int i, j;

for(i = 0; i < m; ++i){
arr[i] = new int
;
}

arr[0][0] = grid[0][0];
for(i = 1; i < m; ++i){
arr[i][0] = arr[i - 1][0] + grid[i][0];
}
for(i = 1; i < n; ++i){
arr[0][i] = arr[0][i - 1] + grid[0][i];
}
for(i = 1; i < m; ++i){
for(j = 1; j < n; ++j){
arr[i][j] = mymin(arr[i - 1][j], arr[i][j - 1]) + grid[i][j];
}
}

j = arr[m - 1][n - 1];

for(i = 0; i < m; ++i){
delete[] arr[i];
}
delete[] arr;
return j;
}
private:
const int &mymin(const int &x, const int &y) {
return (x < y ? x : y);
}
};


Solution2:

  Apparently, this problem still has some space for optimization. The space complexity can be reduced to linear. Since the recurrence relation only involves sum[x - 1][y] and sum[x][y - 1], the calculation of sum[x][y] only depends on the (x - 1)th and xth row (or column, if you like). Therefore, only two rows of extra space are needed for dynamic programming.

  Time complexity is O(m * n), space complexity is O(n).

Accepted code:

// 1WA, 1AC, could've been perfect
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int m, n;

m = grid.size();
if(m <= 0){
return 0;
}

n = grid[0].size();
if(n <= 0){
return 0;
}

int **arr = new int*[2];
int i, j;
int flag;

for(i = 0; i < 2; ++i){
arr[i] = new int
;
}

flag = 0;
arr[flag][0] = grid[0][0];
for(i = 1; i < n; ++i){
arr[flag][i] = arr[flag][i - 1] + grid[0][i];
}
for(i = 1; i < m; ++i){
flag = !flag;
// 1WA here, forgot to add grid[i][0]
arr[flag][0] = arr[!flag][0] + grid[i][0];
for(j = 1; j < n; ++j){
arr[flag][j] = mymin(arr[!flag][j], arr[flag][j - 1]) + grid[i][j];
}
}

j = arr[flag][n - 1];

for(i = 0; i < 2; ++i){
delete[] arr[i];
}
delete[] arr;
return j;
}
private:
const int &mymin(const int &x, const int &y) {
return (x < y ? x : y);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: