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).
动态规划题目
定义一个二维数组记录当前位置到最后一行的数值,然后从下往上算,即s[i][j]代表i,j到最后一行的最小值,这里 选择从下往上是要比从上往下好的
如果从上往下算的话需要判断上一行此处的值是否存在,因为是三角形
* 公式:s[i][j] = t[i][j] + min(s[i+1][j] ,s[i+1][j+1])
* 最后s[0][0]就是最后的结果
public int minimumTotal(List<List<Integer>> triangle) {
int l = triangle.size();
int n = triangle.get(l-1).size();
//特殊情况
if (l ==0 || n==0)
return 0;
int[][] res = new int[l][n];
//初始条件
for (int i = 0;i < n;i++)
{
res[l-1][i] = triangle.get(l-1).get(i);
}
//动态规划主体
for (int i = l-2; i >= 0; i--) {
for (int j = 0;j < triangle.get(i).size();j++)
{
res[i][j] = Math.min(res[i+1][j],res[i+1][j+1]) + triangle.get(i).get(j);
}
}
return res[0][0];
}

[leetcode]120.Triangle三角矩阵从顶到底的最小路径和的更多相关文章

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

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

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

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

  3. LeetCode 120. Triangle (三角形)

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

  4. LeetCode - 120. Triangle

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

  5. leetcode 120 Triangle ----- java

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

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

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

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

  9. [leetcode] 120. Triangle (Medium)

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

随机推荐

  1. 想了解表格问答,我们先看看TA的前世

    摘要:表格问答是一种针对自然语言问题,根据表格内容给出答案的任务. 一.什么是表格问答 表1是一张综艺节目收视率报表,假如你需要了解市场份额在3%以上的综艺节目,你会选择采用什么样的方法? 首先,用肉 ...

  2. Nebula Flink Connector 的原理和实践

    摘要:本文所介绍 Nebula Graph 连接器 Nebula Flink Connector,采用类似 Flink 提供的 Flink Connector 形式,支持 Flink 读写分布式图数据 ...

  3. 20200428_在centos7.2上挂载ntfs和备份文件到移动硬盘

    [root@localhost ~]# fdisk -l 磁盘 /dev/sda:2000.4 GB, 2000398934016 字节,3907029168 个扇区 - 设备 Boot Start ...

  4. jdk版本下载

    oracleJDK oracle各版本下载地址:https://www.oracle.com/technetwork/java/archive-139210.html openJDK 编译好的 ojd ...

  5. PyQt(Python+Qt)学习随笔:QMdiArea多文档界面部件的subWindowActivated信号

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 QMdiArea的subWindowActivated在一个窗口激活( ...

  6. PyQt(Python+Qt)学习随笔:Qt Designer中部件的locale属性

    locale属性用于设置语言环境,包括语言和国家.如果一个部件没有设置语言环境,则使用父对象的语言环境或者默认语言环境(如果这个部件是顶层部件). 可以使用locale()获取部件的语言环境,也可以通 ...

  7. 搭建伪分布式 hadoop3.1.3 + zookeeper 3.5.7 + hbase 2.2.2

    安装包 Hadoop 3.1.3 Zookeeper 3.5.7 Hbase 2.2.2 所需工具链接: 链接:https://pan.baidu.com/s/1jcenv7SeGX1gjPT9RnB ...

  8. python 练习洗牌

    生成随机数需要引入random模块,学习下random模块中常用的几个函数: random.random() 用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.un ...

  9. IAR环境定义位变量标志位 STM8 MSP430通用

    首先建立一个公共点H文件,加入通用代码如下 typedef union { struct { unsigned char b0:1; unsigned char b1:1; unsigned char ...

  10. 题解-Quantifier Question

    Quantifier Question 有长度为 \(n\) 的序列 \(x\{n\}\),有 \(m\) 个条件 \((j_i,k_i)\).有 \(n\) 个待定的条件符 \(Q_i\in\{\f ...