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的更多相关文章
随机推荐
- qt http 下载文件
			
本文章介绍如何利用HTTP从网站上下载文件.在Qt网络编程中,需要用到协议,即HTTP.它是超文本传输协议,它是一种文件传输协议.对于HTTP就不多解释了. 在Qt网络编程中,需要用到协议,即HTTP ...
 - Powershell过滤管道结果
			
通过管道可以过滤某些对象和对象的属性,这个功能很实用,因为很多时候我们并不是对所有的结果感兴趣,可能只会对某些结果感兴趣.如果要过滤对象可以使用Where-Object:如果要过滤对象的属性,可以使用 ...
 - list 操作
			
animals = ["aardvark", "badger", "duck", "emu", "fennec ...
 - H5页面音频自动播放问题
			
最近有这么一个需求,需要在手机加载一个页面的时候,自动播放音乐资源.一般情况下,这个问题也就解决了,但是要保证各种手机上表现一致,那就相当困难了,至少要费点儿周折. 下面有三种常规 ...
 - poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)
			
Description Bessie noted that although humans have many universities they can attend, cows have none ...
 - pyqt一个小例子
			
# -*- coding: utf-8 -*- __author__ = 'Administrator' from PyQt4 import Qt,QtCore,QtGui import sys,ra ...
 - hibernate错题解析
			
01 Hibernate错题分析 解析: 此题目考查的是对Hibernate中交叉连接的理解.HQL支持SQL风格的交叉连接查询,交叉连接适用于两个类之间没有定义任何关联时.在where字句中,通 ...
 - Excel函数大全
			
我们在使用Excel制作表格整理数据的时候,经常要用到它的函数功能来自己主动统计处理表格中的数据.这里整理了Excel中使用频率最高的函数的功能.用法,以及这些函数在实际应用中的实例剖析,并配有具体的 ...
 - Makefile学习(一)变量
			
鉴于之前有一些了解,还有自己的学习习惯,我一上来就看Makefile的变量这一章.主要脉络是根据GNU make中文手册. 第六章:Makefile中的变量 6使用变量 定义:变量是一个名字,代表一个 ...
 - [CSS] :not Selector
			
The CSS :not() selector allows us to exclude a subset of elements matched by our selector. In this e ...