Question

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

Solution 1 -- DP

We define dp[i] to be the smallest sum that must include nums[i]. For easier understanding, we maintain two lists to record dp[i] information. Time complexity O(n^2) and space cost O(n).

 public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() < 1)
return 0;
int size = triangle.size(), result = Integer.MAX_VALUE;
List<Integer> currentList, currentDP, prevDP = new ArrayList<Integer>();
for (int i = 0; i < size; i++) {
currentList = triangle.get(i);
currentDP = new ArrayList<Integer>();
if (i == 0) {
currentDP.add(currentList.get(i));
} else {
for (int j = 0; j <= i; j++) {
int tmpMin;
// Three Cases
if (j == 0)
tmpMin = currentList.get(j) + prevDP.get(0);
else if (j == i)
tmpMin = currentList.get(j) + prevDP.get(j - 1);
else
tmpMin = currentList.get(j) + Math.min(prevDP.get(j), prevDP.get(j - 1));
currentDP.add(tmpMin);
}
}
prevDP = currentDP;
}
// Select minimum number of dp[i]
for (int tmp : prevDP)
result = Math.min(tmp, result);
return result;
}
}

Solution 2 -- Bottom Up

In this way, we need not to consider the three cases discussed above.

 public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() < 1)
return 0;
int size = triangle.size();
int[] dp = new int[size];
for (int i = 0; i < triangle.get(size - 1).size(); i++)
dp[i] = triangle.get(size - 1).get(i);
// Iterate from last second row
for (int i = size - 2; i >= 0; i--) {
List<Integer> tmpList = triangle.get(i);
for (int j = 0; j < tmpList.size(); j++) {
dp[j] = tmpList.get(j) + Math.min(dp[j], dp[j + 1]);
}
}
return dp[0];
}
}

Triangle 解答的更多相关文章

  1. Pascal's Triangle 解答

    Question Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  2. 【PTA|Python】浙大版《Python 程序设计》题目集:第二章

    前言 Hello!小伙伴! 非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出-   自我介绍 ଘ(੭ˊᵕˋ)੭ 昵称:海轰 标签:程序猿|C++选手|学生 简介:因C语言结识编程,随后转入计 ...

  3. Pascal's Triangle II 解答

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

  4. UVa OJ 194 - Triangle (三角形)

    Time limit: 30.000 seconds限时30.000秒 Problem问题 A triangle is a basic shape of planar geometry. It con ...

  5. Leetcode_119_Pascal's Triangle II

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41851069 Given an index k, retu ...

  6. LeetCode题目解答

    LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...

  7. [Leetcode Week8]Triangle

    Triangle 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/triangle/description/ Description Given a t ...

  8. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

  9. [LeetCode] Triangle 三角形

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

随机推荐

  1. Android_Studio 及SDK下载

    Android Studio includes all the tools you need to build apps for Android. DOWNLOAD ANDROID STUDIO 2. ...

  2. 理解 Linux 配置文件分类和使用

    理解 Linux 配置文件分类和使用 本文说明了 Linux 系统的配置文件,在多用户.多任务环境中,配置文件控制用户权限.系统应用程序.守护进程.服务和其它管理任务.这些任务包括管理用户帐号.分配磁 ...

  3. Increasing/ Decreasing Stack

    对于此类问题: 对于元素nums[i],找出往左/右走第一个比它小/大的数字 我们常常用递增栈/递减栈实现. 递增栈实现第一个比它小 递减栈实现第一个比它大 Example: 2 1 5 6 2 3 ...

  4. iOS 推送证书制作 (JAVA/PHP)

    // aps_development.cer 转化成pem openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem ...

  5. mysql常用操作命令

    本章内容:(引用原文:http://www.cnblogs.com/suoning/p/5769141.html) 查看\创建\使用\删除 数据库 用户管理及授权实战 局域网远程连接法 查看\创建\使 ...

  6. HashMap的使用方法及注意事项

    99.Map(映射):Map 的keySet()方法会返回 key 的集合,因为 Map 的键是不能重复的,因此 keySet()方法的返回类型是 Set:而 Map 的值是可以重复的,因此 valu ...

  7. Unity目录结构

    http://www.cnblogs.com/liudq/p/5540051.htmlUnity中有几个默认目录 Unity5.x Resources 项目中默认的资源路径,会直接打包到游戏包中.即使 ...

  8. FlexSlider是一个非常出色的jQuery滑动切换插件

    FlexSlider是一个非常出色的jQuery滑动切换插件,它支持所有主流浏览器,并有淡入淡出效果.适合所有初级和高级网页设计师使用.不过很多人都只是使用默认的参数,今天来说说具体的参数来给大家看看 ...

  9. DataList、Repeater、GridView中的Checkbox取值问题

    先看页面代码 <asp:DataList id="DataList1" runat="server" Width="100%" Rep ...

  10. ckplayer,超酷网页播放器,用于集成在网站中的播放器

    自己在工作中做了一个教学网站,点击左边课程,右边播放视频,经过源代码分析,用的就是这个播放器 网址:http://www.ckplayer.com/ 具体使用播放器网站上说的比较明白 div id=& ...