题目

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.

题解:

一道动态规划的经典题目。需要自底向上求解。

递推公式是: dp[i][j] = dp[i+1][j] + dp[i+1][j+1] ,当前这个点的最小值,由他下面那一行临近的2个点的最小值与当前点的值相加得到。

由于是三角形,且历史数据只在计算最小值时应用一次,所以无需建立二维数组,每次更新1维数组值,最后那个值里存的就是最终结果。

代码如下:

 1 public int minimumTotal(List<List<Integer>> triangle) {
 2     if(triangle.size()==1)
 3         return triangle.get(0).get(0);
 4         
 5     int[] dp = new int[triangle.size()];
 6 
 7     //initial by last row 
 8     for (int i = 0; i < triangle.get(triangle.size() - 1).size(); i++) {
 9         dp[i] = triangle.get(triangle.size() - 1).get(i);
     }
  
     // iterate from last second row
     for (int i = triangle.size() - 2; i >= 0; i--) {
         for (int j = 0; j < triangle.get(i).size(); j++) {
             dp[j] = Math.min(dp[j], dp[j + 1]) + triangle.get(i).get(j);
         }
     }
  
     return dp[0];
 }

Reference:  http://www.programcreek.com/2013/01/leetcode-triangle-java/

Triangle leetcode java的更多相关文章

  1. Pascal's Triangle leetcode java(杨辉三角)

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...

  2. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  3. LeetCode算法题-Pascal's Triangle(Java实现)

    这是悦乐书的第170次更新,第172篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第29题(顺位题号是118).给定非负整数numRows,生成Pascal三角形的第一个 ...

  4. [LeetCode][Java]Triangle@LeetCode

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

  5. [Leetcode][JAVA] Pascal's Triangle I, II

    Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, give ...

  6. leetcode 119 Pascal's Triangle II ----- java

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  7. Pascal's Triangle 2(leetcode java)

    问题描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return ...

  8. Pascal's Triangle II Leetcode java

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...

  9. Regular Expression Matching leetcode java

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

随机推荐

  1. notepad++ 如何选择10000行-20000行之间的文本?

    最近要上传导入一批数据,但是数据太多,一次上传不了,所以就要分批上传,而且数据全部在一个txt里面,这时就想一次复制一部分出来导入,直到导入完成,但是问题来了,数据太多,选择1到10000行,鼠标要拉 ...

  2. Cannot create a new pixel buffer adaptor with an asset writer input that has already started writing'

    reason: '*** -[AVAssetWriterInputPixelBufferAdaptor initWithAssetWriterInput:sourcePixelBufferAttrib ...

  3. 用AB对Webservice做压力测试

    AB工具下载 AB工具介绍见 web压力测试工具 下载ab工具后放到任意目录下,从命令行打开ab.exe 使用以下命令测试Webservice ab -c 100 -n 1000 -p zip.txt ...

  4. stm32的swd接口的烧写协议是否公开的呢?

    stm32的swd接口的烧写协议是否公开的呢? 需要用一台好的示波器来抓才能有足够的存储深度,保证你能够过滤掉那个该死的50clock. 按照Arm的手册,每次转换发送方都需要一个TNR---但是我观 ...

  5. General PE format layout

  6. LPC1800 and LPC4300 MCUs

    LPC1800 Series microcontrollers At 180 MHz, LPC1800 Series microcontrollers combine the industry's f ...

  7. STM32学习日志--使用DMA功能自动更新PWM的输出

    /******************************************************************************* 编译环境: EWARM V5.30 硬 ...

  8. Synchronize Ultimate

    支持多种服务器和主流云网盘进行同步 http://www.icecoldapps.com/ Unlock Code : xda201506 Unlock Code : icecoldapps20150 ...

  9. loading加载和layer.js

    layer.js中的loading加载 l本篇主要介绍layerjs中的loading加载在实际项目中的应用 1.使用的技术 前端:HTML5+CSS3+JS+layer.js 后端:.net 2.遇 ...

  10. Android ListView中按钮监听器设置的解决方案

    在做安卓应用开发的时候很经常会用到ListView,并且每一个Item里面都会有按钮之类的需要进行事件监听的控件.在给按钮添加OnClickListener的时候,一开始很下意识的会想在ListVie ...