一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:给定一个三角矩阵,求从上到下的路径中和最小的路径。每次向下走只能从相邻的数走。

解题思路:Note中提到空间复杂度要为O[n],n为最下面一行数的个数

这种问题一般都会想到动态规划,所以直接往转移方程上想。

记dp[i][j]为(i,j)点到最低端的最小路径和。n为矩阵的深度

则从第n-1行开始,dp[i][j] += min(dp[i+1][j],dp[i+1][j+1]),一路往上计算,最终dp[0][0]即为所求。

于是很快写出代码,10msAC版本。

class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        if(triangle.empty()) return 0;
        vector<vector<int>> dp = triangle;
        int n = dp.size();
        if(n==1) return triangle[0][0];//只有一行的时候直接返回
        for(int i = n-2 ; i >=0 ; i--)
        {
            for(int j = 0 ; j < dp[i].size();j++)
            {
                dp[i][j] += min(dp[i+1][j],dp[i+1][j+1]);//状态转移方程
            }
        }
        return dp[0][0];
    }
};

考虑到优化上述代码,使得空间复杂度更低,满足O(n),其实,只用一个一维数组就能解决问题。

下面的代码时优化后的版本,8msAC.

class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        if(triangle.empty()) return 0;
        int n = triangle.size();
        vector<int> dp = triangle[n-1];//空间复杂度更低
        for(int i = n-2 ; i >=0 ; i--)
        {
            for(int j = 0 ; j < triangle[i].size();j++)
            {
                dp[j] = triangle[i][j]+min(dp[j],dp[j+1]);
            }
        }
        return dp[0];
    }
};

【一天一道LeetCode】#120. 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 - 120. Triangle

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

  3. leetcode 120 Triangle ----- java

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

  4. [LeetCode] 120. Triangle _Medium tag: Dynamic Programming

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

  5. [leetcode 120]triangle 空间O(n)算法

    1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...

  6. Java for LeetCode 120 Triangle

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

  7. [leetcode] 120. Triangle (Medium)

    原题 思路: dp,从下往上依次取得最小的,取到最上面的,就是一条最小的路径. class Solution { public: int minimumTotal(vector<vector&l ...

  8. LeetCode 120. Triangle三角形最小路径和 (C++)

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  9. LeetCode 120. Triangle (三角形最小路径和)详解

    题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...

  10. [leetcode]120.Triangle三角矩阵从顶到底的最小路径和

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

随机推荐

  1. python2.7入门---运算符

        已经分享过变量类型的基本概念了,接下来就研究了一下运算符的基础知识.接下来我们就来看一下内容.举个简单的例子 4 +5 = 9 .例子中,4 和 5 被称为操作数,"+" ...

  2. Java 反射 Method threw 'java.lang.InstantiationException' exception.

    查看这个InstantiationException:异常的api所说的是: 当应用程序试图使用 Class 类中的 newInstance 方法创建一个类的实例,而指定的类对象无法被实例化时,抛出该 ...

  3. win10利用自带的IIS搭建ftp遇到瓶颈,离线求解!!!

  4. IntelliJ IDEA设置统一编码utf-8

    File菜单->Other Settings->Default Settings->File Encodings 全改成utf-8!

  5. pm2进阶使用

    启用集群模式 只需要在启动应用时带上i参数 pm2 start app.js -i max max:意味着PM2将自动检测可用的CPU数量和运行多个进程可以在负载均衡模式(但是不推荐使用) 或者使用j ...

  6. 各种电子面单Api接口免费对接-快宝开放平台

    1.什么是电子面单? 快递公司联合向商家提供的一种通过热敏纸打印输出纸质物流面单的物流服务,并且承载分单自动化算法等数据服务,是快递行业赋能的基础产品和服务. 2.电子面单长什么样? 各快递公司有自己 ...

  7. ubuntu + 不识别无线网卡简易处理方式 + 需有线联网

    进入ubuntu, 连接有线网; 系统设置 -> 软件和更新 -> 附加驱动 -> 找到无线网卡驱动并更新 若无候选的无线网卡驱动,我就没招了.

  8. Lock锁子类了解一下

    前言 回顾前面: 多线程三分钟就可以入个门了! Thread源码剖析 多线程基础必要知识点!看了学习多线程事半功倍 Java锁机制了解一下 AQS简简单单过一遍 只有光头才能变强! 上一篇已经将Loc ...

  9. C/C++静态数组与动态数组的区别

    简介 以下三行代码有什么区别? int a[10]; int *a = (int*)malloc(sizeof(int)*10); int *a = new int[10]; 第一行代码定义a为包含1 ...

  10. 使用Boost program_options控制程序输入

    简介 很多人使用界面来输入数据,本文的程序介绍如何使用Boost的program_options控制输入. 程序中使用了: 1. 短选项 2. 可以指定多个参数的选项 程序 #include < ...