LeetCode_Triangle
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 [
[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, where n is the total number of rows in the triangle.
分析:This problem is more likely to be a (dynamic programming) DP problem,
where a[n][i] = a[n][i]+min(a[n-1][i], a[n-1][i-1]).
Note that in this problem, "adjacent" of a[i][j] means a[i-1][j] and a[i-1][j-1], if available(not out of bound), while a[i-1][j+1] is not "adjacent" element.
The minimum of the last line after computing is the final result.
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = triangle.size();
for( int i = ; i< len ; i++)
for( int j = ; j < triangle[i].size(); j++){
if(j == ){
triangle[i][j] += triangle[i-][j];
continue;
}
if(j == triangle[i].size() -){
triangle[i][j] += triangle[i-][j-];
continue;
}
int val = triangle[i-][j] < triangle[i-][j-] ? triangle[i-][j]
:triangle[i-][j-] ;
triangle[i][j] += val;
}
int minval = triangle[len-][];
for(int i = ; i< triangle[len-].size() ; i++){
if(minval > triangle[len-][i] ) minval = triangle[len-][i];
}
return minval;
}
};
reference :http://yucoding.blogspot.com/2013/04/leetcode-question-112-triangle.html
LeetCode_Triangle的更多相关文章
随机推荐
- DOS的BAT技巧两则
一,杀FF进程 二,删除FF产生的配置文件(这个大了,不小心就会爆盘) taskkill /f /t /im firefox.exe for /d %%i in (C:\Users\cheng\App ...
- 小qyvlik 先看两个视频,和 QtQuick UI 问答
http://edu.csdn.net/course/detail/1042 http://edu.csdn.net/course/detail/335 http://blog.csdn.net/qy ...
- 用Delphi的TIdHttp控件发起POST请求和Java的Servlet响应
http://blog.csdn.net/panjunbiao/article/details/8615880 用Delphi的TIdHttp控件发起POST请求和Java的Servlet响应
- Java并发实现一(并发的实现之Thread和Runnable的区别)
package com.subject01; public class ThreadOrRunnable { public static void main(String[] args) { Syst ...
- Annotation(二)——Hibernate中注解的开发
在利用注解开发数据库持久层以前,需要学习一个规范JPA(Java Persistence API),这也是SUN公司提出的数据库的持久化规范.就类似于JDBC,Servlet,JSP等规范一样.而Hi ...
- Js获取元素样式值(getComputedStyle¤tStyle)兼容性解决方案
因为:style(document.getElementById(id).style.XXX)只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的. 一般js获取内部样式和外部样式使用 ...
- Hadoop,HBase集群环境搭建的问题集锦(四)
21.Schema.xml和solrconfig.xml配置文件里參数说明: 參考资料:http://www.hipony.com/post-610.html 22.执行时报错: 23., /comm ...
- MongoDB[mark]总忘记它们是干啥的
MongoDB集群包括一定数量的mongod(分片存储数据).mongos(路由处理).config server(配置节点).clients(客户端).arbiter(仲裁节点:为了选举某个分片存储 ...
- 写一个段落python代码推理list深浅
主要是针对嵌套列表问题. 列表套列表,究竟子列表那个更深... 这个问题想着就烦.假设嵌套10000万个列表是不是要统计10000个数再排序呢? 最后想了想用 list的extend功能 加上递归函数 ...
- hdu-1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10219 Accepted: 4977 Des ...