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. php学习笔记

    header("Content-Type:text/html;charset=utf-8");//设置中文请求中文 $host = "localhost";// ...

  2. 由python为入口回顾C++的lambda

    lambda是一种匿名函数,python  lambda可以使简单的函数简洁的表达,,C++的lambda使类似嵌套函数的功能得以实现 python的lambda lambda [arg1[,arg2 ...

  3. linux病毒

    linux病毒查杀规范 一.病毒发现 1.ps -A.ps -ef.ps -aux查看是否有异常进程 2.last,lastlog命令可查看最近登录的帐户及时间 3.查看/var/log/messag ...

  4. mxnet实战系列(一)入门与跑mnist数据集

    最近在摸mxnet和tensorflow.两个我都搭起来了.tensorflow跑了不少代码,总的来说用得比较顺畅,文档很丰富,api熟悉熟悉写代码没什么问题. 今天把两个平台做了一下对比.同是跑mn ...

  5. iOS 解压打包静态库命令

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Hannotate SC" } p.p2 { margin: 0.0px ...

  6. 26 Time Management(转)

    01. There is alway time. Time is priorities. 时间常有.时间优先. 02. Days always fill up. 时间总会有的. Only plan f ...

  7. 超高性能的json序列化之MVC中使用Json.Net

    先不废话,直接上代码 Asp.net MVC自带Json序列化 /// <summary> /// 加载组件列表 /// </summary> /// <param na ...

  8. 阿里中间件——diamond

    一.前言 最近工作不忙闲来无事,仔细分析了公司整个项目架构,发现用到了很多阿里巴巴集团开源的框架,今天要介绍的是中间件diamond. 二.diamond学习笔记 1.diamond简介 diamon ...

  9. Backbone框架浅析

    Backbone是前端mvc开发模式的框架.它能够让view和model相分离,让代码结构更清晰简答,开发进度加快,维护代码方便.但是,现在出了一种mvvm框架,它是下一代前端mvc开发模式的框架,代 ...

  10. 向JSP页面输入信息

    /** * ajax responseTEXT write; * @param request * @param response * @param str */ public static void ...