Triangle 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/triangle/description/


Description

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).

Solution

class Solution {
public:
int min(int a, int b) {
return a < b ? a : b;
} int minimumTotal(vector< vector<int> >& triangle) {
int size = triangle.size();
if (size == 0)
return 0;
if (size == 1)
return triangle[0][0];
int** result = new int*[size];
int i, j;
for (i = 0; i < size; i++)
result[i] = new int[size];
for (i = 0; i < size; i++)
result[size - 1][i] = triangle[size - 1][i];
for (i = size - 2; i >= 0; i--) {
for (j = 0; j <= i; j++) {
result[i][j] = min(result[i + 1][j], result[i + 1][j + 1]) + triangle[i][j];
}
} j = result[0][0];
for (i = 0; i < size; i++)
delete [] result[i];
delete [] result;
return j;
}
};

解题描述

这道题是典型的动态规划问题。从最底层开始向上推导,每一步都是求当前的点应该选择什么后续路径才能保证最终的路径权值之和最小。上面是我最开始的解答,时间复杂度为O(n2),空间复杂度为O(n2)。后面重新想了一下,发现其实记录后续路径之和只需要用一维数组就可以了,于是加以修改得到空间复杂度为O(n)的新解:

class Solution {
public:
int min(int a, int b) {
return a < b ? a : b;
} int minimumTotal(vector< vector<int> >& triangle) {
int size = triangle.size();
if (size == 0)
return 0;
if (size == 1)
return triangle[0][0];
int *result = new int[size];
int i, j;
for (i = 0; i < size; i++)
result[i] = triangle[size - 1][i];
for (i = size - 2; i >= 0; i--) {
for (j = 0; j <= i; j++)
result[j] = min(result[j], result[j + 1]) + triangle[i][j];
}
j = result[0];
delete [] result;
return j;
}
};

[Leetcode Week8]Triangle的更多相关文章

  1. LeetCode 120. Triangle (三角形)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  2. [LeetCode] Valid Triangle Number 合法的三角形个数

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

  3. [LeetCode] Largest Triangle Area 最大的三角区域

    You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...

  4. LeetCode Valid Triangle Number

    原题链接在这里:https://leetcode.com/problems/valid-triangle-number/description/ 题目: Given an array consists ...

  5. 【leetcode】Triangle (#120)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  6. [LeetCode][Java]Triangle@LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. LeetCode - 120. Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  8. 【leetcode】triangle(easy)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  9. leetcode 120 Triangle ----- java

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

随机推荐

  1. [USACO18DEC]Cowpatibility(容斥 or bitset优化暴力)

    题面 题意: 给出n个五元组(一个五元组的五个数互不相同),我们称两个五元组不和谐,当且仅当任意元素都不相同,求有多少对五元组不和谐. \(Solution:\) 很容易想到 Ans = 总共对数-和 ...

  2. 用tensorflow实现自然语言处理——基于循环神经网络的神经语言模型

    自然语言处理和图像处理不同,作为人类抽象出来的高级表达形式,它和图像.声音不同,图像和声音十分直觉,比如图像的像素的颜色表达可以直接量化成数字输入到神经网络中,当然如果是经过压缩的格式jpeg等必须还 ...

  3. kaldi学习 - 一脚本流学习工具使用

    目录 yesno训练 先给出整体脚本如下: 分块详解 建立解码脚本 kaldi中脚本东西比较多,一层嵌一层,不易阅读. 本文以yesno为例,直接使用kaldi编译的工具,书写简易训练步骤,方便学习k ...

  4. web3无法安装的额解决方案-----yarn命令安装web3

    凡是可以用 JavaScript 来写的应用,最终都会用 JavaScript 来写. --Atwood定律(Jeff Atwood在2007年提出) yarn命令详解 https://yarnpkg ...

  5. 复合类型的声明——是int *p还是int* p

    我们先来看一条基本类型的声明语句:int a, b, ... 即一条声明语句由一个数据类型(int)和紧随其后的一个变量名列表(a, b, ...)组成 更通用的描述是:一个基本数据类型和紧随其后的一 ...

  6. Linux C++线程池实例

    想做一个多线程服务器测试程序,因此参考了github的一些实例,然后自己动手写了类似的代码来加深理解. 目前了解的线程池实现有2种思路: 第一种: 主进程创建一定数量的线程,并将其全部挂起,此时线程状 ...

  7. user32的使用

    通过代码查询特定的窗口,并在文本框中输入文字然后单击"OK"按钮 需要查找的Dialog 使用Spy++查看窗口信息 通过代码实现功能 class Program { //defi ...

  8. Github & DMCA Takedown Policy

    Github & DMCA Takedown Policy Digital Millennium Copyright Act 数字千年版权法案 https://help.github.com/ ...

  9. 【题解】APIO2007动物园

    首先一眼感受到这题特别的性质……5个?这么小的,感觉就像是状压.脑补了一下,如果没有环的话应该很好做吧……有环怎么办?5真的很小的,随便乱搞肯定也可以.那就放在外面暴力枚举吧.然后正解就出来了. 然而 ...

  10. [poj 1947]树dp+背包问题

    题目链接:http://poj.org/problem?id=1947 看了很多题解都是直接一遍dfs就搞定的方法,但是我实在是没看懂那个转移方程.最后在茫茫博客中终于发现了一个有逻辑的方法,但是复杂 ...