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.

Runtime: 444 ms

动态规划思想: 从上到下一行一行的扫描并加入总体,dp[i]记录每扫描完一行,并途过本行i元素的最短路径,故dp[]记录到目前为止所有路径的长度。O(n) space

public class Solution {

public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {

int[] dp=new int[triangle.size()];

if(triangle.size()==0) return 0;

if(triangle.size()==1) return triangle.get(0).get(0);

dp[0]=triangle.get(0).get(0);

for(int i=1;i<triangle.size();i++){

for(int j=i;j>=0;j--){

if(j==0) dp[0]+=triangle.get(i).get(0);

else if(j<i) dp[j]=triangle.get(i).get(j)+ Math.min(dp[j],dp[j-1]);

else dp[j]=dp[j-1]+triangle.get(i).get(j);

}

}

int ret=Integer.MAX_VALUE;

for(int i=0;i<dp.length;i++){

if(dp[i]<ret) ret=dp[i];

}

return ret;

}

}

[LeetCode][Java]Triangle@LeetCode的更多相关文章

  1. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  4. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. java 分布式锁方案

    第一步,自身的业务场景: 在我日常做的项目中,目前涉及了以下这些业务场景: 场景一: 比如分配任务场景.在这个场景中,由于是公司的业务后台系统,主要是用于审核人员的审核工作,并发量并不是很高,而且任务 ...

  2. 常用的dos命令

    cd 进入一个目录 cd .. 返回上一个目录 dir 遍历目录 上下键 查找输入过的命令 Tab键 命令自动补齐

  3. 启动hadoop,没有启动namenode进程。log4j:ERROR setFile(null,true) call faild.

    启动hadoop,没有启动namenode进程.log4j:ERROR setFile(null,true) call faild.   解决办法: cd /home/hadoop/hadoop-en ...

  4. myeclipse中配置maven

    1.myeclipse中已默认安装maven,首先在window-preferences-myeclipse-maven下找到maven插件,不同的版本位置可能不同,但都可以在window-prefe ...

  5. SHA1算法

    public string SHA1_Hash(string str_sha1_in)        {            SHA1 sha1 = new SHA1CryptoServicePro ...

  6. HttpClient接口测试之会话保持

    HttpClient接口测试之会话保持     HttpClient4.X自带会话保持功能,使用同一个HttpClient未关闭的连接即可保持登陆会话,如果多个HttpClient想要使用一个登陆会话 ...

  7. Canvas画图在360浏览器中跑偏的问题

    问题描述,canvas画图的js代码中编写的是画正方形的代码,结果在360浏览器上变成了长方形,不知道怎么回事,请问各位大神是否遇到过此类问题? <!DOCTYPE html> <h ...

  8. XML序列化与反序列化

    public static class XmlHelper { private static void XmlSerializeInternal(Stream stream, object o, En ...

  9. SDRAM的主要参数

    (1) 容量.SDRAM的容量经常用XX存储单元×X体×每个存储单元的位数来表示.例如某SDRAM芯片的容量为4M×4×8bit,表明该存储器芯片的容量为16 M字节.或128 M bit. (2) ...

  10. s5pv210中断体系

    一.什么是中断? 1.中断的发明是用来解决宏观上的并行需要的.宏观就是从整体上来看,并行就是多件事情都完成了. 2.微观上的并行,就是指的真正的并行,就是精确到每一秒甚至每一刻,多个事情都是在同时进行 ...