Triangle leetcode
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.
解题思路:使用动态规划法。当我们计算第i层的数到底层的最小和时,如果我们知道第i+1层的数到底层最小的和就好算了。即minsum[i][j]=triangle[i]+min( minsum[i+1][j] , minsum[i+1][j+1] );从底层向顶层逐层计算,就能得到最终结果。
本文使用大小为n的数组d记录每一层的结果,达到了O(n)的空间复杂度要求。
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
int s = triangle.size();
if(s != (triangle[s-1].size()))
return -1;
if(s==1)
return triangle[0][0];
int *d = new int[s];
int i,j;
for(i=0;i<s;i++)
d[i]=triangle[s-1][i];
for(i=s-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
d[j]=triangle[i][j]+min(d[j],d[j+1]);
}
}
return d[0];
}
};
Triangle leetcode的更多相关文章
- [LeetCode][Java]Triangle@LeetCode
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Triangle——LeetCode
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Pascal's Triangle leetcode
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Triangle LeetCode |My solution
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Pascal's Triangle leetcode java(杨辉三角)
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- Triangle leetcode java
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- triangle leetcode C++
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode第二天&第三天
leetcode 第二天 2017年12月27日 4.(118)Pascal's Triangle JAVA class Solution { public List<List<Integ ...
随机推荐
- ARPSpoofing教程(三) - 捕获数据包
1: #include"pcap.h" 2: //每次捕获到数据包时,libpcap都会自动调用这个回调函数 3: void packet_handler(u_char *para ...
- mysql 某周的起始和结束日期
转自:http://bbs.csdn.net/topics/370096126 t_table有数据如下:year Week2011 22011 32011 42011 ...
- ubuntu下的ssh
ubuntu默认是没有安装openssh-server的,今天简单的写一下ubuntu上ssh的安装与使用. 一.服务器端 安装 apt-get install openssh-server #安装 ...
- 利用WSCF进行契约先行的Web Services开发
http://www.cnblogs.com/goody9807/archive/2007/06/05/772107.html 什么是契约先行(Contract-First)? 如果说一个新的软件开发 ...
- 京东分布式MySQL集群方案介绍
背景 数据库作为一个非常基础的系统,任何一家互联网公司都会使用,数据库产品也很多,有Oracle.SQL Server .MySQL.PostgeSQL.MariaDB等,像SQLServer/Ora ...
- LED数码管显示实验
1.代码: #include <reg52.h>typedef unsigned char u8;typedef unsigned int u16;sbit seg_sel = P1 ...
- Extjs GridPanel用法详解
Extjs GridPanel 提供了非常强大数据表格功能,在GridPanel可以展示数据列表,可以对数据列表进行选择.编辑等.在之前的Extjs MVC开发模式详解中,我们已经使用到了GridPa ...
- C#--使用存储过程
过程: 1.连接数据库 2.设置执行类型为存储过程,区别于SQL语句 3.得到执行结果,返回值.输出参数等 //连接数据库 string connstr = "Data Source=ZGC ...
- 编译安装chkrootkit出现的问题
tar xf chkrootkit.tar.gz cd chkrootkit-* make sense的时候出现make: *** [strings-static] Error 1,解决办法:yum ...
- win8.1企业版 IIS8.5 安装php5.5.18详细图文
最近为了做测试需要在电脑上安装php 环境如下 系统 win8.1 企业版 IIS 8.5 PHP:5.5.18 php-5.5.18-nts-Win32-VC11-x64 完整文件名 注意IIS 下 ...