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.

 class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
// Start typing your C/C++ solution below
// DO NOT write int main() function }
};

答题模板

思路:假设三角形共有n行,题目中看出第i行共有i个元素。从top到bottom,我们只考虑minimum path的最后一步是停在了这n个元素的哪一个上面。用数组min_sum(k)表示最后一行到达第k个元素的最小路径(min_sum总长度为n),然后找出min_sum(k)中最小的元素即可。注意一下自上而下递推min_sum时候的递推公式,代码如下:

 class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
// Start typing your C/C++ solution below
// DO NOT write int main() function int n = triangle.size();
if ( == n) return ;
if ( == n) return triangle[][]; int *min_sum = new int[n];
for(int i=; i<n;i++)
min_sum[i] = ; min_sum[]=triangle[][];
for(int i=;i<n;i++){
for(int j=triangle[i].size()-; j>=;j--){
if (==j){
min_sum[j] += triangle[i][];
}else if (triangle[i].size()-==j){
min_sum[j] = min_sum[j-]+triangle[i][j];
}else{
min_sum[j] = min(min_sum[j-], min_sum[j])+triangle[i][j];
}
}
} int minTotal = min_sum[];
for(int i=;i<n;i++){
minTotal = min(minTotal, min_sum[i]);
} delete[] min_sum;
return minTotal;
} int min(int a, int b){
return (a>b?b:a);
}
};

Answer

注意:一开始的时候内部j那个循环是正着写,后来发现这样有个问题,就是有可能会使用已经变动的过的值去更新下一列。

[leetcode.com]算法题目 - Triangle的更多相关文章

  1. [leetcode.com]算法题目 - Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  2. [leetcode.com]算法题目 - Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  3. [leetcode.com]算法题目 - Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. [leetcode.com]算法题目 - Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  5. [leetcode.com]算法题目 - Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  6. [leetcode.com]算法题目 - Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  7. [leetcode.com]算法题目 - Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  8. [leetcode.com]算法题目 - Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. class Solution { public: int sqr ...

  9. [leetcode.com]算法题目 - Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

随机推荐

  1. ubuntu下安装maven(转载)

    下载maven http://maven.apache.org/download.cgi 解压 tar -xzvf apache-maven-3.0.5-bin.tar.gz 配置环境变量 sudo ...

  2. 【Java】生成图形验证码

    本章介绍一个能生成比较好看的图形验证码类 生成验证码工具类 package com.util; import java.awt.Color; import java.awt.Font; import ...

  3. syslog系统日志、事件日志分析、EventLog Analyzer

    syslog系统日志.事件日志分析.EventLog Analyzer Eventlog Analyzer是用来分析和审计系统及事件日志的管理软件,能够对全网范围内的主机.服务器.网络设备.数据库以及 ...

  4. canvas 实现赛车游戏

    一:创建画布 <canvas width="200" height="500" id="canvas" style="bor ...

  5. ES6通过使用babel兼容到ie9

    1.打开这个地址: https://unpkg.com/babel-standalone@6/babel.min.js 新建babel.min.js,把代码复制进去,然后在jq项目里引用. 2.在下面 ...

  6. CodeCraft-19 and Codeforces Round #537 (Div. 2) D 多重排列 + 反向01背包 + 离线处理

    https://codeforces.com/contest/1111/problem/D 多重排列 + 反向01背包 题意: 给你一个字符串(n<=1e5,n为偶数),有q个询问,每次询问两个 ...

  7. boost-使用property_tree来解析xml、json

    property_tree是一个保存了多个属性值的树形数据结构,可以用来解析xml.json.ini.info文件.要使用property_tree和xml解析组件的话需要包含"boost/ ...

  8. windows socket扩展函数

    1.AcceptEx() AcceptEx()用于异步接收连接,可以取得客户程序发送的第一块数据. BOOL AcceptEx( _In_  SOCKET       sListenSocket,   ...

  9. java Concurrent包学习笔记(一):ExecutorService

    一.介绍 ExecutorService是java.util.concurrent包中的一个线程池实现接口.其有两个实现类: 1)ThreadPoolExecutor:普通线程池通过配置线程池大小,能 ...

  10. C++STL 容器比较

    Vector的使用场景:比如软件历史操作记录的存储,我们经常要查看历史记录,比如上一次的记录,上上次的记录,但却不会去删除记录,因为记录是事实的描述. deque的使用场景:比如排队购票系统,对排队者 ...