Leetcode120 Triangle (DP经典题)

【学习体会】Lighttools8.4.0:简单光学系统实例 添加球型光源(SphereSource)和表面接收器: 对光线模拟的参数进行设置: 光线追踪-模拟输入 要追踪的光线总数,决定模拟的准确度,总数越多,准确度越高,当然耗时更多 预览光线数量是模拟之后可以看到的示意光线(黑线),一般是100~1000,随意设置 点击,开始模拟: (模拟结果) 如果光线太多,则会出现,可以看到剩余时间 照度分析: 分析-照度显示-光栅图表 ... 阅读详情
  1. Triangle
    Medium
    8.7K
    503
    Companies
    Given a triangle array, return the minimum path sum from top to bottom.

For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row.

Example 1:

Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
Output: 11
Explanation: The triangle looks like:
2
3 4
6 5 7
4 1 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
Example 2:

Input: triangle = [[-10]]
Output: -10

Constraints:

1 <= triangle.length <= 200
triangle[0].length == 1
triangle[i].length == triangle[i - 1].length + 1
-104 <= triangle[i][j] <= 104

Follow up: Could you do this using only O(n) extra space, where n is the total number of rows in the triangle?

解法1:最直接的DP。

class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        int n = triangle.size();
        if (n == 0) return 0;
        
        vector<vector<int>> dp(n, vector<int>(n, 0));
        dp[0][0] = triangle[0][0];
        //line i (start from 0) has i + 1 numbers
        for (int i = 1; i < n; i++) {
            dp[i][0] = dp[i - 1][0] + triangle[i][0];
            dp[i][i] = dp[i - 1][i - 1] + triangle[i][i];
            for (int j = 1; j < i; j++) {
                dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j]) + triangle[i][j];
            }
        }
        int res = dp[n - 1][0];
        for (int i = 1; i < n; i++) {
            if (res > dp[n - 1][i]) res = dp[n - 1][i];
        }
        return res;
    }
};

解法2:滚动数组实现空间优化

class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        int n = triangle.size();
        if (n == 0) return 0;
        
        vector<vector<int>> dp(2, vector<int>(n, 0));
        dp[0][0] = triangle[0][0];
        //line i (start from 0) has i + 1 numbers
        for (int i = 1; i < n; i++) {
            dp[i % 2][0] = dp[(i - 1) % 2][0] + triangle[i][0];
            dp[i % 2][i] = dp[(i - 1) % 2][i - 1] + triangle[i][i];
            for (int j = 1; j < i; j++) {
                dp[i % 2][j] = min(dp[(i - 1) % 2][j - 1], dp[(i - 1) % 2][j]) + triangle[i][j];
            }
        }
        int res = dp[(n - 1) % 2][0];
        for (int i = 1; i < n; i++) {
            if (res > dp[(n - 1) % 2][i]) res = dp[(n - 1) % 2][i];
        }
        return res;
    }
};

解3:
类似01背包的空间优化。注意j的顺序是从大到小。

class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        int n = triangle.size();
        if (n == 0) return 0;
        
        vector<int> dp(n, INT_MIN);
        dp[0] = triangle[0][0];
        //line i (start from 0) has i + 1 numbers
        for (int i = 1; i < n; i++) {
            // j 的处理是从大到小 
            dp[i] = dp[i - 1] + triangle[i][i];           
            for (int j = i - 1; j > 0; j--) {
                dp[j] = min(dp[j - 1], dp[j]) + triangle[i][j];
            }
            dp[0] = dp[0] + triangle[i][0]; 
        }
        int res = dp[0];
        for (int i = 1; i < n; i++) {
            if (res > dp[i]) res = dp[i];
        }
        return res;
    }
};
《QT+PCL 第一章》:点云读取显示 点云读取显示一、核心代码二、QT设置三、效果展示 一、核心代码 二、QT设置 三、效果展示 阅读详情

相关推荐

别光看原理图了!手把手教你用Altium Designer搞定STM32F411最小系统的PCB布局(附电源/晶振/复位电路实战)

本文详细介绍了使用Altium Designer进行STM32F411最小系统PCB布局的实战技巧,涵盖电源分配网络优化、高速信号布线及系统级布局策略。通过规范的工程结构建立、关键设计规则预设和核心电路模块布局,帮助工程师提升PCB设计效率与信号完整性。特别针对STM32最小系统的电源、晶振和复位电路提供了专业解决方案。

weixin_42653691的博客 175

【LeetCode】120. Triangle

120. Triangle 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 ...

Microstrong 3163

STM32CubeMX定时器进阶玩法:用编码器模式实现电机转速测量(附避坑清单)

本文详细介绍了如何使用STM32CubeMX配置定时器的编码器模式实现电机转速测量,包括硬件连接、CubeMX配置关键步骤、转速计算算法及常见问题解决方案。特别针对工业控制和机器人开发中的高精度需求,提供了实用的避坑清单和优化技巧,帮助开发者高效完成项目。

joy55的博客 211

LeetCode120—Triangle

LeetCode120—Triangle

上善若水 528

利用按键精灵实现批量截图与智能命名的高效工作流

本文详细介绍了如何利用按键精灵软件构建高效的批量截图与智能命名自动化工作流。通过基础脚本编写、循环逻辑实现及高级命名策略,解决手动操作耗时易错的问题,帮助用户实现一键自动截图、智能保存与文件重命名,大幅提升软件测试、教程制作及数据报告等场景的工作效率。

weixin_29268267的博客 574

TCN+iTransformer时间序列预测实战:从风电功率预测到源码解析(附完整代码)

本文深入探讨了TCN与iTransformer融合模型在时间序列预测,特别是风电功率预测中的应用。通过解析iTransformer‘倒置’视角的创新架构与TCN强大的局部时序捕捉能力,文章提供了完整的融合方案设计、源码实现及实战调优经验,旨在构建更强大的下一代预测引擎。

weixin_29062671的博客 269

Leetcode 120 Triangle(DP)

题目连接:Leetcode 120 Triangle解题思路:dp[i][j] += min(dp[i+1][j], dp[i+1][j+1])。class Solution { public: int minimumTotal(vector&lt;vector&lt;int&gt;&gt;&amp; triangle) { int m = triangle.size(); fo...

不慌不忙、不急不躁 494

使用ShaderGraph制作URP各向异性

URP上用shaderGraph实现各向异性效果,解决只能用shader的问题

dreamform的专栏 1102

[leetcode] 120. Triangle

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

Learning from the mistakes 179

Halcon: 获取仿射矩形-rectangle2的四个顶点、四条边的中点

Halcon 工业应用实用教程中的一个例子: * CenterY : 仿射矩形中心y * CenterX: 仿射矩形中心x * Phi: 仿射矩形角度 * Len1: 仿射矩形宽的一半 * Len2: 仿射矩形高的一半 * CornerY: 仿射矩形端点y * CornerX: 仿射矩形端点x * LineCenterY: 仿射矩形边中点y * LineCenterx: 仿射矩形边中点x * 矩...

ruotianxia的博客 8579

Python全栈入门到实战【基础篇 22】生成器与迭代器:底层逻辑与实战场景

Python迭代器与生成器:惰性求值的高效实践 摘要:本文深入解析Python中迭代器和生成器的核心机制与应用场景。迭代器通过实现__iter__()和__next__()方法实现惰性求值,适合处理大数据集和无限序列。生成器作为迭代器的简化实现,通过yield关键字自动实现迭代器协议,代码更简洁。文章详细对比了可迭代对象与迭代器的区别,演示了自定义迭代器和生成器函数的实现方式,并介绍了生成器表达式这一高效替代列表推导式的方案。通过斐波那契数列等实例,展示了惰性求值在内存优化方面的优势,以及生成器在流式数据处

那些看似不起波澜的日复一日,会在某天让你看到坚持的意义 3282

【LeetCode 面试经典150题】120. Triangle 三角形

Given a triangle array, return the minimum path sum from top to bottom.For each step, you may move to an adjacent number of the row below. More formally, if you are on index on the current row, you may move to either index or index on the next row.给定一个三

qq_43513394的博客 539

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

【LeetCode-Java实现】120.Triangle

120.Triangle题目描述思路实现代码 题目描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. Example, given the following triangle [ ----- [...

IT Crowd的博客 303

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

LeetCode --- 120. Triangle

120. Triangle Difficulty: Medium 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 ...

MissXy_的博客 465

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

120. Triangle**

120. Triangle** https://leetcode.com/problems/triangle/ 题目描述 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, g...

珍妮的选择的博客 279

Leetcode 120. Triangle

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

hello world 209

【Leetcode】120. 三角形(Triangle)

Leetcode - 120 Triangle (Medium) 题目描述:给定一个三角形形状的集合,从上到下依次向邻接位置移动,求移动的最小路径和。 [ [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). ...

厉兵秣码 301

LeetCode算法题之120. Triangle(Medium)【Python3题解】

题目描述: 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 The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bo

DaMoWangZQ的博客 398

【LEETCODE】120- Triangle [Python]

【LEETCODE】120- Triangle [Python]

aliceyangxi1987的博客 2185

【DP】LeetCode 120. Triangle

LeetCode 120. Triangle Solution1:我的答案 真费劲啊!!! 啊!!!!!! class Solution { public: int minimumTotal(vector&lt;vector&lt;int&gt;&gt;&amp; triangle) { if (!triangle.size() || !t...

Allenlzcoder的博客 369

leetcode-120. Triangle

leetcode-120. Triangle题目: 思路就是传统的DP,只不过传统的思路都是使用矩阵去做,这里也是类似的,但是需要注意下标是否准确就好。

千念飞羽的专栏 496

【leetcode】【medium】120. Triangle​​​​​​​

120. Triangle 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], ...

lemonade13的博客 100

【LeetCode】120. Triangle 解题报告(Python)

【LeetCode】120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址https://leetcode.com/problems/triangle/description/ 题目描述: Given a triangle, find the minimum path...

负雪明烛 2541

leetcode 120.triangle(动态规划经典题)

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

luoshaohong的博客 252

LeetCode 120 Java解法 使用DP

import java.util.List; /** * @author pauljy * @program LearnJava * @description * @date 2019-09-25 18:43 */ public class Leet120 { /** * @description: 三角形结构的输入,由一个空间复杂度为O(n^2)的解法入手,由底向...

baidu_25364411的博客 374

Leetcode120-Triangle

这是一道典型的dp入门题 题目描述 给定一个三角形,找到从上到下的最小路径和,在每一步可以移动到下一行的相邻数字。 示例 [ [2], [3,4], [6,5,7], [4,1,8,3] ] 代码 class Solution { public: int minimumTotal(vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt;&amp;amp; triangle) { ...

yangke 765
上一篇: BLE 学习小结
下一篇: C++实现两个线程交替打印奇数和偶数,一直到10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值