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

解题思路:

DP问题,用一个dp数组滚动下来即可,JAVA实现如下:

    public int minimumTotal(List<List<Integer>> triangle) {
int[] dp = new int[triangle.size()];
dp[0]=triangle.get(0).get(0);
if(triangle.size()>=2){
dp[1]=triangle.get(1).get(1)+dp[0];
dp[0]+=triangle.get(1).get(0);
}
for (int i=2;i<triangle.size();i++) {
int left = dp[0];
int right = dp[1];
dp[0] += triangle.get(i).get(0);
for (int j = 1; j <= i - 1; j++) {
dp[j] = triangle.get(i).get(j) + Math.min(left, right);
left = right;
right = dp[j + 1];
}
dp[i] = left + triangle.get(i).get(i);
left = dp[0];
right = dp[1];
}
for (int i = 0; i < dp.length - 1; i++)
if (dp[i] < dp[i + 1])
dp[i + 1] = dp[i];
return dp[dp.length - 1];
}

Java for LeetCode 120 Triangle的更多相关文章

  1. leetcode 120 Triangle ----- java

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

  2. LeetCode 120. Triangle (三角形)

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

  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. Java实现 LeetCode 120 三角形最小路径和

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

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

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

  7. [leetcode] 120. Triangle (Medium)

    原题 思路: dp,从下往上依次取得最小的,取到最上面的,就是一条最小的路径. class Solution { public: int minimumTotal(vector<vector&l ...

  8. LeetCode 120. Triangle三角形最小路径和 (C++)

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  9. LeetCode 120. Triangle (三角形最小路径和)详解

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

随机推荐

  1. 【mybatis】从一个错误,看mybatis中的#和$的区别

    事情的发展是这样的: 因为一个需求,需要在java中拼接出一个完整的sql语句,然后将整条sql语句传递给mybatis执行. mapper.java是这样的: int insertMaster(Wo ...

  2. [置顶] django快速获取项目所有的URL

    django快速获取项目所有的URL django1.10快速获取项目所有的URL列表,可以用于权限控制 函数如下: import re def get_url(urllist , parent='' ...

  3. 测试Apache服务器及httpd: Could not reliably determine the server's fully qualified domain name解决办法

    测试Apache服务器: 重启apache: sudo /usr/local/apache/bin/apachectl restart 若出现错误: httpd: Could not reliably ...

  4. opengl interface

    glTranslate()是移动坐标系,比如glTranslate(-1.5,0,0),之后你画的图就是在屏幕左边1.5个单位~glRotation()是做旋转的,第一个参量是angle,后面3个分别 ...

  5. Hadoop一些问题总结

    1.运行mr程序出错 connecting to resoucemanager retrying .... retrying ..... 原因是没有启动yarn或者启动失败 2.初始化工作目录结构 h ...

  6. MySQL监控工具——innotop

    MySQL监控工具--innotop innotop是一个mysql数据库实时监控工具,其功能强大,信息种类繁多,很能体现数据库的状态. 它实际上是一个perl脚本,整合show status/sho ...

  7. vs:如何添加.dll文件

    Newtonsoft.Json.dll  一个第三方的json序列化和反序列化dll,转换成json供页面使用,页面json数据转换成对象,供.net使用 Mysql.Data.dll  mysql数 ...

  8. 安装配置 Kafka Manager 分布式管理工具

    Kafka Manager 特性,它支持以下内容(官方译解): 管理多个群集容易检查集群状态(主题,消费者,偏移量,经纪人,副本分发,分区分配)运行首选副本选举使用选项生成分区分配,以选择要使用的代理 ...

  9. hibernate 配置文件无自动提示

    在编辑 *.hbm.xml 文件时,myeclipse 带有自动提示功能,但 eclipse 是没有自动提示功能的.需要自己手工加上:           1.打开项目中任意一个 *.hbm.xml ...

  10. [jjzhu学java]之solr4.9同步mysql数据

    Solr是一个高性能,採用Java5开发,基于Lucene的全文搜索server.同一时候对其进行了扩展,提供了比Lucene更为丰富的查询语言,同一时候实现了可配置.可扩展并对查询性能进行了优化,而 ...