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. 【前端阅读】——《JavaScript应用开发技术详解指南》摘记&思维导图

    读这本书,我主要关注三个部分:JavaScript内置函数,程序调试以及Ajax基础.由于多是介绍基本概念,所以,采用思维导图的方式,做了一个梳理,以下就是精简的主要内容. 注:转载请注明出处

  2. haifeng

    [root@localhost 桌面]# yum list|grep wubi ibus-table-chinese-wubi-haifeng.noarch -.el7 base ibus-table ...

  3. 115. distinct subsequence leetcode python

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  4. Direct-X学习笔记--纹理映射

    一.介绍 之前学习了如何绘制物体,还画了个DX自带的茶壶,然而这个东东并不怎么好看....离我们现实的物体简直相隔千里. 仅仅能说像美术他们用来写生的模型...那么要怎么样才干让我们的东西看起来更像真 ...

  5. 数据结构基础-Hash Table详解(转)

    理解Hash 哈希表(hash table)是从一个集合A到另一个集合B的映射(mapping). 映射是一种对应关系,而且集合A的某个元素只能对应集合B中的一个元素.但反过来,集合B中的一个元素可能 ...

  6. 【Excle数据透视】如何创建一个数据透视表

    数据透视表可以汇总.分析.浏览和提供工作表数据或外部数据源的汇总数据 创建方法一 通过”插入”创建,具体操作请看下图: 创建方法二 通过快捷键创建 使用[ALT+D+P]创建 首先按下alt键,然后依 ...

  7. Android · PendingIntent学习

    Intent 是及时启动,intent 随所在的activity 消失而消失 PendingIntent用于处理即将发生的事情.比如在通知Notification中用于跳转页面,但不是马上跳转.   ...

  8. C#:用SqlBulkCopy来实现批量插入数据

    SqlBulkCopy是.net2.0的新特性,平时用的很少,但是其功能却是非常强大,对于批量插入数据性能非常优越 代码 /// /// bulk插入/// private void BulkInse ...

  9. mybatis性能优化之降低数据库连接

    做性能优化的最重要的功能就是降低数据库的交互.非常多程序猿一般在开发的时候仅仅考虑简单的实现功能,无论业务简单复杂,仅仅要实现即可. mybatis有个重要的功能就是考虑在联合查询时技巧: <? ...

  10. Redis(九):使用RedisTemplate访问Redis数据结构API大全

    RedisTemplate介绍 spring封装了RedisTemplate对象来进行对redis的各种操作,它支持所有的 redis 原生的api. RedisTemplate在spring代码中的 ...