MCPcopy Create free account
hub / github.com/Ainevsia/Leetcode-Rust / minPathSum

Method minPathSum

64. Minimum Path Sum/Solution.cpp:16–32  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14class Solution {
15public:
16 int minPathSum(vector<vector<int>>& grid) {
17 if (grid.size() == 0) return 0;
18 if (grid[0].size() == 0) return 0;
19 int m = grid.size(), n = grid[0].size();
20 // deal with the first line
21 for (int i = 1; i < m; i ++) {
22 grid[0][i] += grid[0][i - 1];
23 }
24 for (int i = 1; i < m; i ++) {
25 // deal with the first colomn
26 grid[i][0] += grid[i - 1][0];
27 for (int j = 1; j < n; j ++) {
28 grid[i][j] = min(grid[i][j - 1], grid[i - 1][j]) + grid[i][j];
29 }
30 }
31 return grid[m - 1][n - 1];
32 }
33};
34
35int main() {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected