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. 虚拟现实-VR-UE4-构建光照显示光照构建失败,Swarm启动失败

    闲的无聊折腾,发现想构建光照的时候,总是显示失败 如下图 百度许久,有大神指出是我在编译源码的的时候没有将其中的某个模块编译进去,只需要重新编译摸个模块就好 在UE4 的sln文件下,会看到一个Unr ...

  2. 02-Mysql数据库----初识

    什么是数据(Data) 描述事物的符号记录称为数据,描述事物的符号既可以是数字,也可以是文字.图片,图像.声音.语言等,数据由多种表现形式,它们都可以经过数字化后存入计算机 在计算机中描述一个事物,就 ...

  3. AM5728通过GPMC接口与FPGA高速数据通信实现

    硬件:AM5728开发板:Artix-7开发板软件:Linux am57xx-evm 4.4.19:Vivado 2015.2作者:杭州矢志信息科技有限公司邮箱:admin@sysjoint.com ...

  4. 重写page的OnInit(学习中总结的)

    在写b/s框架的系统的时候,我们会发现,我们经常会在不同的网页中验证Session是否存在,,而我这里没有用Session,用的是MemCache技术,其实它就是键值对. 只不过将Memcache中的 ...

  5. OpenCV尺寸调整

    #include<cv.h> #include<highgui.h> int main(int argc, char** argv) { IplImage* img = cvL ...

  6. 关于debian配置的问题汇总

    debian的apache多域名配置: https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-ho ...

  7. css 3 filter

    css 3 filter image & PS effect https://api-platform.com#COMPANIES

  8. javasisst & JAVA8

    今天在服务器上启动tomcat7的时候,提示如下异常: java.io.IOException: invalid constant type: 15 具体看是javasisst抛出来的. 系统运行环境 ...

  9. 配合JAVA的AJAX使用

    概要 Ajax是“Asynchronous JavaScript and XML”的简称,即异步的JavaScript和XML. readyState属性用来返回当前的请求状态,有五个可选值.分别是0 ...

  10. Hibernate常用方法之_查询

    1.使用session的get方法 public User getUser(int id){ Session session = null; User user = null; try { sessi ...