LeetCode Triangle 120 DP问题
2015-06-05 16:11
453 查看
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
The minimum path sum from top to bottom is
2 + 3 + 5 +
1 = 11).
Note:
Bonus point if you are able to do this using only O(n) extra space, wheren is the total number of rows in the triangle.
For example, given the following triangle
[ [2], [3,4], [6,5,7], [4,1,8,3] ]
The minimum path sum from top to bottom is
11(i.e.,
2 + 3 + 5 +
1 = 11).
Note:
Bonus point if you are able to do this using only O(n) extra space, wheren is the total number of rows in the triangle.
class Solution { public: int minimumTotal(vector<vector<int> > &triangle) { int i,j; for(i=triangle.size()-2;i>=0;i--) { for(j=0;j<i+1;j++) triangle[i][j]=min(triangle[i+1][j],triangle[i+1][j+1]); } return triangle[0][0]; } };
相关文章推荐
- ACM开始心声
- getApplicationContext()和Activity.this
- Linux iostat监测IO状态
- 服装企业IT管理 应该这样当
- vsftp实现匿名用户文件可上传不能删除或覆盖
- java.sql.SQLException: 无效的列索引
- cygwin编码设置(修复调用ping等windows自带命令乱码)
- iOS旋转图片锯齿问题
- iOS加密算法(DES、AES、MD5、SHA512、Base64)
- android webview 配置及常见问题
- 搜索浅谈(Elasticsearch和Lucene4分享)
- Thinkphp2.2 config.inc.php常用配置
- 代码的未来读书笔记<二>
- 数据结构笔记三
- jquery判断至少有一个checkbox被选中的方法
- 在SVN上实现代码风格自动检查
- 递归删除
- 随意拖拽的控件
- spring data redis使用示例
- pcap文件格式