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. docker容器中启动kvm虚拟机

    .安装docker yum install docker systemctl start docker.service systemctl enable docker.service .拉取cento ...

  2. UGUI 代码 动态添加 Event Trigger 的事件

    Additionally, if you need more than just the events provided by default, I'd suggest instead attachi ...

  3. [整理]docker内部时区修改的两种方法

    方法一 终端执行 date命令,查看宿主服务器的时区是否正确 如果正确: 执行 docker cp /etc/localtime 容器ID:/etc/localtime 将本地时间拷贝到docker内 ...

  4. springboot 学习笔记(二)--- properties 配置

    springboot可以提供了多种方式配置properties. 一.Java System.setProperty(k, v) System.setProperty("myname&quo ...

  5. JDK的弃儿:Vector、Stack、Hashtable、Enumeration

    随着JDK的发展,一些设计缺陷或者性能不足的类库难免会被淘汰,最常见的就是Vector.Stack.HashTable和Enumeration了. Vector(@since 1.0) 首先看看Vec ...

  6. CSS优化压缩

    顾名思义缩写有简写意思,那就总结一下CSS缩写知识点.为什么要让CSS属性缩写?1.简化代码.一些CSS属性简写可以减少CSS代码从而减少CSS文件的占用字节.加快网页下载速度和网页加载速度.2.优化 ...

  7. VisualStudio2010项目转换为VisualStudio2005项目:解决方案和工程项目文件转换方法(2)

    因为我现在不喜欢把一篇博客写的很长很长,这篇博客是接着上一篇博客来写的.上一篇文章我很详细的说明了修改项目文件解决方案的过程.这篇文章我就说说项目中的项目文件该怎么修改.因为我平日里主要做的是ASP. ...

  8. [洛谷P2626]斐波那契数列(升级版)

    题目大意:请你求出第$n$个斐波那契数列的数$mod 2^{31}$之后的值.并把它分解质因数. 题解:乱搞 卡点:1.忘记取模 C++ Code: #include<cstdio> #i ...

  9. 2018-2-6考试(COCI2014/2015 Contest#5)

    T1:FUNGHI(1s,32M,50pts)得分:50 题意:给你8个数组成一个环,要你求出其中连续的4个数,让它们的和最大 题解:暴力求出每一连续4个数之和,比较一下就好 标签:模拟 C++ Co ...

  10. Eclipse中的引用项目报Could not find *.apk!解决办法

    百度上很多关于Could not find *.apk!这种编译报错的解决帖子,但是笔主在这里主要说一下在 引用工程项目的场景 下报这个错误消息的问题(不影响本项目的正常编译运行!). 笔主刚从谷歌上 ...