[LeetCode] Minimum Path Sum

简介: This is a typical DP problem. Suppose the minimum path sum of arriving at point (i, j) is S[i][j], then the state equation is S[i][j] = min(S[i - 1][j], S[i][j - 1]) + grid[i][j].

This is a typical DP problem. Suppose the minimum path sum of arriving at point (i, j) is S[i][j], then the state equation is S[i][j] = min(S[i - 1][j], S[i][j - 1]) + grid[i][j].

Well, some boundary conditions need to be handled. The boundary conditions happen on the topmost row (S[i - 1][j] does not exist) and the leftmost column (S[i][j - 1] does not exist). Suppose grid is like [1, 1, 1, 1], then the minimum sum to arrive at each point is simply an accumulation of previous points and the result is [1, 2, 3, 4].

Now we can write down the following (unoptimized) code.

 1 class Solution {
 2 public:
 3     int minPathSum(vector<vector<int>>& grid) {
 4         int m = grid.size();
 5         int n = grid[0].size(); 
 6         vector<vector<int> > sum(m, vector<int>(n, grid[0][0]));
 7         for (int i = 1; i < m; i++)
 8             sum[i][0] = sum[i - 1][0] + grid[i][0];
 9         for (int j = 1; j < n; j++)
10             sum[0][j] = sum[0][j - 1] + grid[0][j];
11         for (int i = 1; i < m; i++)
12             for (int j = 1; j < n; j++)
13                 sum[i][j]  = min(sum[i - 1][j], sum[i][j - 1]) + grid[i][j];
14         return sum[m - 1][n - 1];
15     }
16 };

As can be seen, each time when we update sum[i][j], we only need sum[i - 1][j] (at the current column) and sum[i][j - 1] (at the left column). So we need not maintain the full m*nmatrix. Maintaining two columns is enough and now we have the following code.

 1 class Solution {
 2 public:
 3     int minPathSum(vector<vector<int>>& grid) {
 4         int m = grid.size();
 5         int n = grid[0].size();
 6         vector<int> pre(m, grid[0][0]);
 7         vector<int> cur(m, 0);
 8         for (int i = 1; i < m; i++)
 9             pre[i] = pre[i - 1] + grid[i][0];
10         for (int j = 1; j < n; j++) { 
11             cur[0] = pre[0] + grid[0][j]; 
12             for (int i = 1; i < m; i++)
13                 cur[i] = min(cur[i - 1], pre[i]) + grid[i][j];
14             swap(pre, cur); 
15         }
16         return pre[m - 1];
17     }
18 };

Further inspecting the above code, it can be seen that maintaining pre is for recovering pre[i], which is simply cur[i] before its update. So it is enough to use only one vector. Now the space is further optimized and the code also gets shorter.

 1 class Solution {
 2 public:
 3     int minPathSum(vector<vector<int>>& grid) {
 4         int m = grid.size();
 5         int n = grid[0].size();
 6         vector<int> cur(m, grid[0][0]);
 7         for (int i = 1; i < m; i++)
 8             cur[i] = cur[i - 1] + grid[i][0]; 
 9         for (int j = 1; j < n; j++) {
10             cur[0] += grid[0][j]; 
11             for (int i = 1; i < m; i++)
12                 cur[i] = min(cur[i - 1], cur[i]) + grid[i][j];
13         }
14         return cur[m - 1];
15     }
16 };

 

目录
相关文章
Leetcode Minimum Depth of Binary Tree (面试题推荐)
计算树的最小深度 很简单的一道题,只需要遍历一次树,到叶子节点的时候计算一下深度和当前最小深度比较,保存最小值就行。 我在这用了一个全局变量 mindepth。总感觉我这代码写的不够简练,求更精简的方法。
54 0
LeetCode Contest 178-1368. 使网格图至少有一条有效路径的最小代价 Minimum Cost to Make at Least One Valid Path in a Grid
LeetCode Contest 178-1368. 使网格图至少有一条有效路径的最小代价 Minimum Cost to Make at Least One Valid Path in a Grid
LeetCode 433. Minimum Genetic Mutation
现在给定3个参数 — start, end, bank,分别代表起始基因序列,目标基因序列及基因库,请找出能够使起始基因序列变化为目标基因序列所需的最少变化次数。如果无法实现目标变化,请返回 -1。
73 0
LeetCode 433. Minimum Genetic Mutation
|
存储
LeetCode 329. Longest Increasing Path in a Matrix
给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
81 0
LeetCode 329. Longest Increasing Path in a Matrix
|
存储 索引
LeetCode 310. Minimum Height Trees
对于一个具有树特征的无向图,我们可选择任何一个节点作为根。图因此可以成为树,在所有可能的树中,具有最小高度的树被称为最小高度树。给出这样的一个图,写出一个函数找到所有的最小高度树并返回他们的根节点。
78 0
LeetCode 310. Minimum Height Trees
LeetCode 209. Minimum Size Subarray Sum
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。
127 0
LeetCode 209. Minimum Size Subarray Sum
|
人工智能 算法
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
|
4月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
5月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
65 6
|
5月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
133 2