博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Minimum Path Sum】cpp
阅读量:7220 次
发布时间:2019-06-29

本文共 1002 字,大约阅读时间需要 3 分钟。

题目:

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.

Note: You can only move either down or right at any point in time.

代码:

class Solution {public:    int minPathSum(vector
>& grid) { if ( grid.empty() ) return 0; const int m = grid.size(); const int n = grid[0].size(); vector
dp(n, INT_MAX); dp[0] = 0; for ( int i=0; i

tips:

典型的“DP+滚动数组”,时间复杂度O(m*n),空间复杂度O(n)。

=============================================

第二次,用偷懒的做法了,二维dp直接写了。

class Solution {public:    int minPathSum(vector
>& grid) { if ( grid.empty() ) return 0; int dp[grid.size()][grid[0].size()]; fill_n(&dp[0][0], grid.size()*grid[0].size(), 0); dp[0][0] = grid[0][0]; for ( int i=1; i

 

转载于:https://www.cnblogs.com/xbf9xbf/p/4550146.html

你可能感兴趣的文章
Html唤起手机APP,如果有就唤起,如果没有就跳到下载页。
查看>>
Java中File类如何扫描磁盘所有文件包括子目录及子目录文件
查看>>
VC++ 限制窗口的大小范围的方法
查看>>
结对开发-返回一个整数数组中最大子数组的和(首尾相接版)
查看>>
meanshift-聚类
查看>>
不要if else的编程
查看>>
rn.ShowDialog() == DialogResult.OK
查看>>
20160519
查看>>
SCU 3132(博弈)
查看>>
正则表达式
查看>>
delete archivelog all 无法彻底删除归档日志?
查看>>
Redis五大数据类型
查看>>
大型分布式网站架构技术总结
查看>>
矩阵求导与投影梯度相关问题
查看>>
SVN
查看>>
C语言编程写的一个http下载程序(王德仙)2012-04-08
查看>>
CCF201409-3 字符串匹配(100分)
查看>>
UVALive2203 UVa10042 Smith Numbers【质因数分解+素数判定+数位之和】
查看>>
Project Euler Problem 9: Special Pythagorean triplet
查看>>
HDU5701 中位数计数【中位数】
查看>>