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:
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:
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); } };
相关文章推荐
- LeetCode 63 Minimum Path Sum
- [Leetcode 59] 64 Minimum Path Sum
- leetcode_c++:Minimum Path Sum (064)
- Leetcode 64(Minimum Path Sum)
- LeetCode Minimum Path Sum
- [LeetCode]Minimum Path Sum
- [Leetcode] Minimum Path Sum
- LeetCode 064 Minimum Path Sum
- leetcode64-Minimum Path Sum(最小路径和)
- 【LeetCode】Minimum Path Sum
- Leetcode--Minimum Path Sum
- LeetCode:Minimum Path Sum
- LeetCode 64: Minimum Path Sum
- LeetCode——minimum-path-sum
- LeetCode 064 Minimum Path Sum
- Minimum Path Sum——LeetCode
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
- leetcode——MinimumPathSum
- LeetCode:Minimum Path Sum
- LeetCode 之 Minimum Path Sum