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

    1.迭代器遍历 import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public c ...

  2. 再也不担心写出臃肿的Flink流处理程序啦,发现一款将Flink与Spring生态完美融合的脚手架工程-懒松鼠Flink-Boot

    目录 你可能面临如下苦恼: 接口缓存 重试机制 Bean校验 等等...... 它为流计算开发工程师解决了 有了它你的代码就像这样子: 仓库地址:懒松鼠Flink-Boot 1. 组织结构 2. 技术 ...

  3. python核心高级学习总结5--------python实现线程

    在代码实现上,线程的实现与进程的实现很类似,创建对象的格式都差不多,然后执行的时候都是用到start()方法,与进程的区别是进程是资源分配和调度的基本单位,而线程是CPU调度和分派的基本单位.其中多线 ...

  4. 第15.29节 PyQt(Python+Qt)入门学习:containers容器类部件QScrollArea滚动区域详解

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 Scroll Area提供了一个呈现在其他部件上的可滚动区域视图,滚动区域用于显示框架内的 ...

  5. PyQt(Python+Qt)学习随笔: QAbstractItemView的dragDropMode属性

    老猿Python博文目录 老猿Python博客地址 一.概述 dragDropMode属性用于控制视图拖放事件的处理方式,其类型为枚举类型DragDropMode. 二.枚举类型DragDropMod ...

  6. PyQt(Python+Qt)学习随笔:Designer中的QDialogButtonBox增加自定义按钮的方法

    在Qt Designer中可以预先定义标准按钮,相关支持的标准按钮请见<PyQt(Python+Qt)学习随笔:Designer中的QDialogButtonBox的StandardButton ...

  7. 【转载—“光荣之路”公众号】Bug预防体系(上千bug分析后总结的最佳实践)

    web常见产品问题及预防 测试人员在每次版本迭代中,会对项目的整体质量有一个把控,对于项目常见的问题,开发经常犯的错误都会有所了解,为了避免或者减少这样的错误或不规范的事情再发生,测试人员可以整理构建 ...

  8. 学习一下 SpringCloud (一)-- 从单体架构到微服务架构、代码拆分(maven 聚合)

    一.架构演变 1.系统架构.集群.分布式系统 简单理解 (1)什么是系统架构? [什么是系统架构?] 系统架构 描述了 在应用程序内部,如何根据 业务.技术.灵活性.可扩展性.可维护性 等因素,将系统 ...

  9. js 转为整数之Number()、parseInt()、parseFloat()区别

    一:Number() 如果是Boolean值,true和false值将分别被转换为1和0. 如果是数字值,只是简单的传入和返回. 如果是null值,返回0. 如果是undefined,返回NaN. 如 ...

  10. javascript常用继承方式.

      //原型链继承 function Parent() { this.name = 'per'; } function Child() { this.age = 20; } Child.prototy ...