leetcode: Triangle

【机器学习|评价指标8】混淆矩阵(Confusion Matrix)、Cohen‘s Kappa 系数、详解,平均交并比(mIoU)、列联表(Contingency Table)附代码。 【机器学习|评价指标8】混淆矩阵(Confusion Matrix)、Cohen's Kappa 系数、详解,平均交并比(mIoU)、列联表(Contingency Table)附代码。 阅读详情

题意:

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

典型DP:

用 int 二维数组 dp[i][j] 表示:以第 i 行第 j 个元素为终点的路径的和,最后只要求出最后一行的最小值就是题目的解了。

唯一要注意的就是在每一行的第一个和最后一个元素的时候,只有一条路可以走,还有就是判断异常值,其他没有什么特别的。

下面上代码:

public int minimumTotal(List<List<Integer>> triangle) {
        if(triangle==null || triangle.get(0)==null)
		{
			return 0;
		}
		
		int n = triangle.size();
		int m = triangle.get(n-1).size();
		if(n==1){return triangle.get(0).get(0);}
		
		int dp[][] = new int[n][m];
		
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
			dp[i][j] = 0;
		
		dp[0][0] = triangle.get(0).get(0);
		for(int i=1;i<n;i++)
		{
			int i_size = triangle.get(i).size();
			for(int j=0;j<i_size;j++)
			{
				int temp = triangle.get(i).get(j);
				if(j==0){
					dp[i][j]= temp + dp[i-1][j];
				}else if(j==i_size-1){
					dp[i][j]= temp + dp[i-1][j-1];
				}else{
					dp[i][j]= temp + dp[i-1][j-1] < temp + dp[i-1][j]?
							temp + dp[i-1][j-1]:temp + dp[i-1][j];	
				}	
			}
		}
		int min = dp[n-1][0];
		for(int i=1;i<m;i++)
		{
			min = min<dp[n-1][i]?min:dp[n-1][i];
		}
		return min;
    }


区块链技术概述:从比特币到Web3.0 区块链技术正重塑数字世界的信任机制。本文系统梳理了区块链的发展路径:从比特币的PoW共识和UTXO模型,到以太坊的智能合约和账户体系,直至Web3.0的去中心化互联网愿景。文章详细解析了区块链的核心特性(去中心化、不可篡改等)和数据结构,通过流程图展示了比特币交易生命周期和智能合约执行流程,并对比了Web2.0与Web3.0的关键差异。最后,用Python代码实现了包含区块类、交易处理和挖矿机制的简易区块链系统,为理解区块链底层原理提供了实践参考。 阅读详情

相关推荐

OpenCV卡尺工具实战:5步搞定工业级圆与直线拟合(附完整C++源码)

本文详细介绍了使用OpenCV卡尺测量工具实现工业级圆与直线拟合的5个关键步骤,包括核心原理、环境配置、边缘点提取优化、鲁棒性几何拟合实现及QT集成技巧。通过模块化C++源码和实战案例,帮助开发者快速掌握高精度尺寸测量技术,适用于机械零件、PCB板等工业检测场景。

uran的专栏 843

LeetCode 120 : Triangle

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

Cecilia_whf的博客 350

保姆级教程:在Windows/Linux双系统下搞定Microsemi Libero SoC v11.8安装与白银版License激活

本文提供了一份详细的Microsemi Libero SoC v11.8在Windows/Linux双系统下的安装与白银版License激活指南。涵盖环境准备、安装流程、License跨平台配置及常见问题排查,帮助开发者高效搭建FPGA开发环境。

weixin_30603633的博客 420

Leetcode: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], [

littlebirdman的专栏 1595

OpenCloudOS9操作系统搭建Confluence8.2.1+Jira企业级WIKI

Atlassian Confluence是一个专业的企业知识管理与协同软件,可用于构建企业wiki

背锅神童的博客 1022

手把手教你学Simulink——基础储能管理场景实例:基于Simulink的压缩空气储能(CAES)系统简化模型仿真

本文介绍了基于Simulink的压缩空气储能(CAES)系统简化模型仿真方法。CAES作为大规模长时储能方案,相比锂电池具有成本低、寿命长等优势。文章详细讲解了AA-CAES系统组成、热力学建模理论,以及Simulink实现流程,包括储气室、压缩机、膨胀机等核心模块的建模方法。通过8小时充电(50MW)和6小时放电(40MW)的仿真,验证了系统可实现60%往返效率。该模型为理解CAES系统运行机制提供了实用工具,并指出热管理、储气室特性等工程实践要点,为新型电力系统储能规划提供了仿真基础。

MHD0815的博客 171

leetcode笔记:Pascal's Triangle II

该题要求只输出第k行的元素值,并且要求空间复杂度为`O(k)`,因此,采用的方法是只使用一个定长的数组,用于存放每一行的元素值,对于每个新的行,可对原先存放的行从后往前扫描,主要分为以下三种情况: 最后一个元素,直接等于1; 对于下标为i的中间元素,有:`result[i] = result[i-1] + result[i]`; 第一个元素,直接等于1;

liyuefeilong的专栏 1299

YOLO26涨点改进| CVPR 2026 | 独家创新首发、特征融合改进篇 | 引入 LFSB 差分双维注意融合模块,通过交替特征融合与分离策略,能够精准区分目标特征,顶会助力YOLO26有效涨点

本文提出LSFB差分双维注意融合模块,用于改进YOLO26目标检测模型。该模块通过融合-分离交替策略和差分注意力机制,有效提升模型在复杂场景下的特征分离能力,特别是在处理重叠物体或反射干扰时表现优异。LSFB采用双向投影、双维注意力计算和跨流减法算子,实现特征共享与独立提取的平衡。实验表明,该模块能显著减少特征混淆,提高检测精度和鲁棒性。文章还详细介绍了模块实现的核心代码、YAML配置文件修改方法以及三种改进方案,为YOLO26模型性能提升提供了创新解决方案。

Ai缝合怪、助力小伙伴高效发论文 326

Leetcode: Triangle

最近都在复习英语,看见看得头都大了,而且阅读越做分数越低!换个环境,做做Leetcode试题! 题目: 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

给永远比拿愉快 1574

智能车图像处理16-进阶篇8--右环岛状态机与补线

这篇文章主要讲述智能车竞赛中如何判断右环岛并且进行状态补线。

taiyuezyh的博客 1万+

一文吃透 SAP IDoc 状态码:从 TEDS1 到 WE02 排障,再到 ABAP 自动化监控

在 SAP 集成场景里,IDoc 往往是最常见、也最容易被低估的一条生命线:采购订单、发票、主数据、库存移动……只要系统之间要稳定地交换结构化业务数据,IDoc 就经常登场。真正让人头疼的,并不是 IDoc 本身,而是它在链路里到底走到了哪一步、卡在了哪里、该找谁背锅。这篇文章把常用的 IDoc 状态码按出站、入站拆开讲清楚,并且把每个状态对应的真实世界故障现象、排查入口、以及可以落地的 ABAP 监控代码串起来,让你看到状态码就能想到动作。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 405

leetcode: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],

le119126的专栏 444

不提交也不释放的事务,数据库连接还有可能被复用

摘要 实验发现Spring事务管理下,未提交事务的数据库连接池连接仍可被复用,这与传统认知相悖。通过配置HikariCP连接池(最大2连接)并设计测试接口,观察到未提交事务的连接会被后续请求复用。这可能导致GTID错误(当MySQL开启GTID且混用事务/非事务表时)以及数据修改丢失问题。研究揭示了Spring事务管理机制与连接池交互的特殊行为,对高并发系统设计具有重要启示。

蔡定努 176

Leetcode 119: Pascal‘s Triangle II

119.Pascal's Triangle II Easy Given an integerrowIndex, return therowIndexthrow of the Pascal's triangle. Noticethat the row index starts from0. In Pascal's triangle, each number is the sum of the two numbers directly above it. Follow up: Coul...

roufoo的博客 185

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

【LeetCode】解题120:Triangle

LeetCode解题 120:Triangle Problem 120: Triangle [Medium]解题思路Solution (Java) Problem 120: Triangle [Medium] Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac...

Fayedily的博客 302

IEM模型,地表粗糙度模拟

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

【LeetCode】解题118:Pascal's Triangle

LeetCode解题 118:Pascal's Triangle Problem 118: Pascal's Triangle [Easy]解题思路Solution (Java) Problem 118: Pascal’s Triangle [Easy] Given a non-negative integer numRows, generate the first numRows of Pasc...

Fayedily的博客 286

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

LeetCode:Triangle

TriangleTotal Accepted:71791Total Submissions:235313Difficulty:MediumGiven a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.Fo...

walker lee 2002

leetcode:Triangle

如图,在一个三角形二维数组中有一系列数,求出从顶层到底层最小和的路径。

恰同学少年 941

Leetcode:Triangle

URLhttps://leetcode.com/problems/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 t

zhumingyuan111的博客 289

leetcode:Pascal's Triangle

经典题目,杨辉三角,输入行数,生成杨辉三角的数组

恰同学少年 1118

LeetCode: 120. Triangle

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

杨领well的专栏 680

LeetCode: pascal's Triangle

LeetCode: pascal's Triangle LeetCode: pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ ...

weixin_33877885的博客 150

[LeetCode] 119: Triangle

[Problem]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],

1.02^365的成长裂变 3663

LeetCode OJ:Triangle

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]

王川的私房菜 1215

Leetcode: Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle. Pascal's triangle就是传说中的杨辉三角,这题只要知道杨辉三角的性质就很好做。杨辉三角中第i 行第j 个元素等于第 i-1 行第 j 个元素和 第 j-1 个元素之和。 public class Solution { public Lis

此间少年 244

LeetCode : 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,

Jilong Sun(@Uchicago) 659
上一篇: leetcode majority element
下一篇: 哈希的原理
MR_LUXY
博客等级 码龄16年 20粉丝 43原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值