-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum_path_sum.cpp
More file actions
73 lines (68 loc) · 1.91 KB
/
minimum_path_sum.cpp
File metadata and controls
73 lines (68 loc) · 1.91 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* =====================================================================================
*
* Filename: minimum_path_sum.cpp
*
* Description: 64. 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.
*
* Version: 1.0
* Created: 04/21/19 11:45:39
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <vector>
class Solution
{
public:
int minPathSum(std::vector<std::vector<int>>& grid)
{
if (grid.empty() || grid[0].empty())
{
return 0;
}
std::vector<std::vector<int>> sums(grid.size() + 1,
std::vector<int>(grid[0].size() + 1, 0));
for (int i = grid.size() - 1; i >= 0; i--)
{
const auto& row = grid[i];
auto& curr = sums[i];
auto& prev = sums[i + 1];
for (int j = row.size() - 1; j >= 0; j--)
{
if (i == (grid.size() - 1))
{
curr[j] = curr[j + 1] + row[j];
}
else if (j == (row.size() - 1))
{
curr[j] = prev[j] + row[j];
}
else
{
curr[j] = std::min(prev[j], curr[j + 1]) + row[j];
}
}
}
return sums[0][0];
}
};
int main(int argc, char* argv[])
{
std::vector<std::vector<int>> grid = {
{1, 3, 1},
{1, 5, 1},
{4, 2, 1},
};
int sum = Solution().minPathSum(grid);
printf("Minimum path sum: %d\n", sum);
return 0;
}