problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
lintcode::min_path_sum::Solution类 参考

#include <lintcode.h>

静态 Public 成员函数

static int minPathSum (vector< vector< int > > &grid)
 

详细描述

在文件 lintcode.h129 行定义.

成员函数说明

◆ minPathSum()

int lintcode::min_path_sum::Solution::minPathSum ( vector< vector< int > > &  grid)
static
参数
grida list of lists of integers
返回
: An integer, minimizes the sum of all numbers along its path

在文件 lintcode.cpp248 行定义.

248 {
249 const unsigned int m = grid.size();
250 const unsigned int n = grid[0].size();
251 auto *dp = new int *[m + 1];
252 for(int i = 0; i <= m; i++) {
253 dp[i] = new int[n + 1];
254 memset(dp[i], INT_MAX, (n + 1) * sizeof(int));
255 }
256 dp[1][1] = grid[0][0];
257 for(int i = 1; i <= m; i++) {
258 for(int j = 1; j <= n; j++) {
259 if(i == 1 && j == 1) {
260 continue;
261 }
262 unsigned int min = dp[i - 1][j];
263 if(dp[i][j - 1] < min) {
264 min = dp[i][j - 1];
265 }
266 dp[i][j] = grid[i - 1][j - 1] + min;
267 }
268 }
269 const auto ans = dp[m][n];
270 for(int i = 0; i <= m; i++) {
271 delete[] dp[i];
272 }
273 delete[] dp;
274 return ans;
275 }

该类的文档由以下文件生成: