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.


题目标签:Array, Dynamic Programming

  题目给了我们一个三角的array list, 让我们从中找到最小值的路线。一旦看到这种每一个点可以选择路线的,有不同可能性的,大多都是 DP 问题。一开始自己写了一个 复制一个 三角的array list, 然后从上到下的遍历,把每一个种可能都存到新的三角 array list。但是用了过多的 extra space, 没有满足它的要求。 所以通过后发现速度太慢,想了一会就放弃了,去网上找答案。有时候如果认认真真自己想一道这样类似的题目,可能会用3,4个小时的时间,而且,还不一定想的出正确答案,所以通常花费1个多小时的话,就果断去找答案了。

  只用O(n) 空间的方法:

    设立一个 n+1 array,然后从最下面一行倒着遍历回第一行,对于n+1 array里面的数字,取它和它后面 两者之中 小的那一个数字,加上 在三角里相对应位置的数字, 存入n+1 array。 基本思想是,利用倒着遍历的方法,可以把每一个数字的最优选择path 存入n+1 array里,当遍历回第一行的时候,因为只有一个数字,那么取得的path 一定是最优的那一个,换句话说,就是minimum path sum。

举例: 1 5 3 2 为最优路线

          

            

       

4             8                  对于每一个数字,选下面相邻2个中小的那一个 +  自己   一直遍历回第一行

0       0      0       0      0        设立的1d array 相当于在这个位置

设立n + 1 array: 从三角最下面遍历回最上面, 每次在1d array 里 取相邻两个种小的那一个 +  三角里对应位置的数字

0  0  0  0  0  初始为0,为什么要多一个呢,因为每次都是取当前和后面一个数字比较,所以当遍历到第四个的时候,需要和第五个比较

遍历三角开始, 结合上面三角图形来看

 4    1    8    3    0  遍历三角4183 -> 因为1d array里都是0,所以每次取相邻里小的那一个的话,都是0 + 三角里对应位置的数字,相当于 把最后一行复制一下

 7    6   10   3    0  遍历三角657   -> 从之前存的里面,挑对应相邻两个中小的那个 + 自己 存入1d array

 9   10  10   3    0  遍历三角34

11  10  10   3    0  遍历三角2 -> 得到答案11

Java Solution:

Runtime beats 58.69%

完成日期:08/27/2017

关键词:Array, Dynamic Programming

关键点:从下向上遍历,把最优路线存入1d array

 class Solution
{
public int minimumTotal(List<List<Integer>> triangle)
{
// create a k+1 size array
int [] arr = new int[triangle.size() + 1]; // iterate from last row to first row
for(int i=triangle.size()-1; i>=0; i--)
{
// iterate each row from left to right
for(int j=0; j<triangle.get(i).size(); j++)
{
arr[j] = Math.min(arr[j], arr[j+1]) + triangle.get(i).get(j);
}
} /* the answer is the first number because we start from last row back to first row
and first row is just one number */
return arr[0];
}
}

参考资料:

https://discuss.leetcode.com/topic/22254/7-lines-neat-java-solution

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

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. 120 Triangle 三角形最小路径和

    给出一个三角形(数据数组),找出从上往下的最小路径和.每一步只能移动到下一行中的相邻结点上.比如,给你如下三角形:[     [2],    [3,4],   [6,5,7],  [4,1,8,3]] ...

  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:三角形最小路径和

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

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

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

  7. leetcode 120 Triangle ----- java

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

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

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

随机推荐

  1. python函数式编程,列表生成式

    1.python 中常见的集中存储数据的结构: 列表 集合 字典 元组 字符串 双队列 堆 其中最常见的就是列表,字典. 2.下面讲一些运用循环获取字典列表的元素 >>> dic={ ...

  2. java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector

    在使用C3P0连接池的时候,发现了这个错误-.原来要使用C3P0的使用,不仅仅要导入c3p0-0.9.2-pre1.jar这个jar包,还要导入mchange-commons-0.2.jar这个jar ...

  3. Activiti-04-.Spring integration

    ProcessEngineFactoryBean <beanid="processEngineConfiguration"class="org.activiti.s ...

  4. 7z命令行 极限压缩指令

    摘抄自http://www.cnblogs.com/qanholas/archive/2011/10/03/2198487.html 7za a -t7z bag.7z "/home/fil ...

  5. (转)添加PROPAGATION_REQUIRES_NEW 事务没有产生作用

    最近在做事务添加时  发现自己的事务没有新建,上网查到   仅用作收藏. 其二  注意  事务的注解  应该在 内层的事务上面 一.描述 Spring遇到嵌套事务时,当被嵌套的事务被定义为" ...

  6. Java为什么把String设计成不可变的(immutable)

    在java中,String是字符串常量,可以从内存,同步机制,数据结构等方面分析 1:字符串中常量池的需要 String不同于普通基础变量类型的地方在于对象.java中的字符串对象都保存在字符串常量池 ...

  7. Apache Spark 2.2.0 中文文档 - SparkR (R on Spark) | ApacheCN

    SparkR (R on Spark) 概述 SparkDataFrame 启动: SparkSession 从 RStudio 来启动 创建 SparkDataFrames 从本地的 data fr ...

  8. 动易CMS - 设为首页代码和加入收藏代码(兼容各种浏览器)

    注意: 这里虽然说是兼容,但是有些浏览器的设置就是不支持用js来把页面设为首页,加入收藏夹,只能让用户手动去在浏览器或者按键去设置这些功能,这里说的兼容是指当浏览器有这个设置的时候js会有提示.   ...

  9. activemq的安装与使用

    一.activemq的安装 环境:CentOS 6.JDK8 1. 确保系统已安装了可用的jdk版本2. 从网上下载 Linux 版的 ActiveMQ( apache-activemq-5.11.1 ...

  10. python之路第四篇(基础篇)

    一.冒泡算法实现: 方法一: li = [13,33,12,80,66,1] print li for m in range(4): num1 = li[m] num2 = li[m+1] if nu ...