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. 【czy系列赛】czy的后宫6 && bzoj1044 [HAOI2008]木棍分割

    题目描述 众所周知的是丧尸czy有很多妹子(虽然很多但是质量不容乐观QAQ),今天czy把n个妹子排成一行来检阅.但是czy的妹子的质量实在--所以czy看不下去了.检阅了第i个妹子会增加czy a[ ...

  2. poj3349(哈希+链地址法)

    给出N个六边形的6个边长,问其中是否有完全相同的两个六边形,完全相同包括边的长度和位置都要相同.边给出的顺序是逆时针或者顺时针的. 给每个6边形一个哈希值,方法是对6条边长度的平方和取模 #inclu ...

  3. python海明距离 - 5IVI4I_I_60Y的日志 - 网易博客

    python海明距离 - 5IVI4I_I_60Y的日志 - 网易博客 python海明距离   2009-10-01 09:50:41|  分类: Python |  标签: |举报 |字号大中小  ...

  4. c++在函数后面加const

    非静态成员函数后面加const(加到非成员函数或静态成员后面会产生编译错误),表示成员函数隐含传入的this指针为const指针,决定了在该成员函数中,任意修改它所在的类的成员的操作都是不允许的(因为 ...

  5. pyqt listview基础学习01

    from decimal import * from PyQt4.QtGui import * from PyQt4.Qt import * from PyQt4.QtCore import * im ...

  6. 解决getJdbcTemplate往oracle数据库中插入数据返回主键出错问题

    我们使用Spring中的JdbcDaoSupport往Mysql中插入数据并返回主键代码,我们使用的mysql数据库,主键在数据库中设置为自增长:该类继承自JdbcDaoSupport,所以能直接使用 ...

  7. Analyzing the Analyzers 分析分析师 —— 数据科学部门如何建

    很多牛逼的公司都宣称在建立数据科学部门,这个部门该如何组建,大家都在摸石头过河. O‘reilly Strata今年 六月份发布了报告 <Analyzing the Analyzers>, ...

  8. python 弄github代码库列表

    1.底        项目要求,征求github的repo的api,为了能够提取repo对数据进行分析. 研究一天.最终克服该问题,較低下.     由于github的那个显示repo的api,列出了 ...

  9. JSTL学习笔记(核心标签)

    一.JSTL标签分类: 核心标签 格式化标签 SQL标签 XML标签 JSTL函数 二.核心标签       引用方式:<%@ taglib prefix="c" uri=& ...

  10. 502 Proxy Error。The ISA Server denied the specified Uniform Resource Locator (URL).

    问题:部署好项目,在IE地址栏输入http://localhost/myweb/index.aspx,回车后报错:   解释: 试图访问的页面出现问题,无法显示此页面. 尝试下列: 刷新页: 单击“刷 ...