-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0064-Minimum_Path_Sum.cpp
More file actions
97 lines (89 loc) · 2.51 KB
/
0064-Minimum_Path_Sum.cpp
File metadata and controls
97 lines (89 loc) · 2.51 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*******************************************************************************
* 0064-Minimum_Path_Sum.cpp
* Billy.Ljm
* 27 Mar 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/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.
*
* ===========
* My Approach
* ===========
* We'll use dynamic programming to calculate the minimum path sum to endpoints
* that are increasingly downwards and rightwards. This avoids recalculating
* overlapping paths, such as DR, RD, etc.
*
* This would have a time complexity of O(m*n), where m and n are the width and
* height of the grid respectively. And I'll overwrite/mutate the input grid to
* achieve a space complexity of O(1).
******************************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
class Solution {
public:
/**
* Finds the path from the top left of a grid of integers to the bottom right,
* which minimises the sum of elements along the path
*
* @param grid grid of integers to traverse
*
* @return minimum sum of elements connecting grid[0][0] to grid[m][n]
*/
int minPathSum(std::vector<std::vector<int>>& grid) {
int m = grid[0].size(); // width of grid
int n = grid.size(); // height of grid
std::cout << m << ",," << n << std::endl;
for (int steps = 1; steps < m + n - 1; steps++) { // total down+right steps
for (int i = 0; i < std::min(n, steps + 1); i++) { // downards steps
int j = steps - i; // rightwards steps
// fill up grid[i][j]
if (j >= m) {
continue;
}
else if (i == 0) {
grid[i][j] += grid[i][j - 1];
}
else if (j == 0) {
grid[i][j] += grid[i - 1][j];
}
else {
grid[i][j] += std::min(grid[i - 1][j], grid[i][j - 1]);
}
}
}
return grid[n-1][m-1];
}
};
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (int i = 0; i < v.size(); i++) {
os << v[i] << ",";
}
os << "\b]";
return os;
}
/**
* Test cases
*/
int main(void) {
Solution sol;
std::vector<std::vector<int>> grid;
// test case 1
grid = { {1,3,1}, {1,5,1}, {4,2,1} };
std::cout << "minPathSum(" << grid << ") = "
<< sol.minPathSum(grid) << std::endl;
// test case 2
grid = { {1,2,3}, {4,5,6} };
std::cout << "minPathSum(" << grid << ") = "
<< sol.minPathSum(grid) << std::endl;
return 0;
}