Triangle 解答
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 解答的更多相关文章
- Pascal's Triangle 解答
Question Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- 【PTA|Python】浙大版《Python 程序设计》题目集:第二章
前言 Hello!小伙伴! 非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出- 自我介绍 ଘ(੭ˊᵕˋ)੭ 昵称:海轰 标签:程序猿|C++选手|学生 简介:因C语言结识编程,随后转入计 ...
- Pascal's Triangle II 解答
Question Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...
- UVa OJ 194 - Triangle (三角形)
Time limit: 30.000 seconds限时30.000秒 Problem问题 A triangle is a basic shape of planar geometry. It con ...
- Leetcode_119_Pascal's Triangle II
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41851069 Given an index k, retu ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- [Leetcode Week8]Triangle
Triangle 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/triangle/description/ Description Given a t ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- [LeetCode] Triangle 三角形
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
随机推荐
- wikioi1369 xth 砍树
题目描述 Description 在一个凉爽的夏夜,xth 和 rabbit 来到花园里砍树.为啥米要砍树呢?是这样滴, 小菜儿的儿子窄森要出生了.Xth这个做伯伯的自然要做点什么.于是他决定带着 r ...
- bzoj1755 [Usaco2005 qua]Bank Interest
Description Farmer John made a profit last year! He would like to invest it well but wonders how muc ...
- 提高xshell使用效率
1.快速命令集. 2.鼠标复制粘贴设置. 3.配色方案. 4.esc切换到英文输入. 设置入口:
- 第六百二十一天 how can I 坚持
好歹都是一些集成的框架,也懒得去深入研究了,微信小程序,今天体验了一个,虽然内心非常相信张小龙,但是还是不太看好,但是感觉确实需要一种方式去简化应用程序的开发,今天把没用的app都删了,太耽误时间了, ...
- pyqt 配置文件例子学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import sys,datetime from PyQt4.QtC ...
- UIScreen类
CGRect screenBounds = [ [UIScreen mainScreen]bounds];//返回的是带有状态栏的Rect NSLog(@"%@", NSStrin ...
- NFinal学习笔记 02—NFinalBuild
在学习NFinal的过程中发现在线.net编译器Web版—— NFinalBuild 什么是NFinalBuild呢?它就是帮助我们简单又快速的更新我们网站的一种编译器,我们不用再只为了更新.net网 ...
- border-radius讲解1
如今CSS3中的border-radius出现后,让我们没有那么多的烦恼了,首先制作圆角图片的时间是省了,而且其还有多个优点:其一减少网站的维护的工作量,少了对图片的更新制作,代码的替换等等;其二.提 ...
- poi HSSFRichTextString 对cell中的每一段文字设置字体
HSSFRichTextString ts= new HSSFRichTextString(" 经审核,我司已同意其出库申请.请你部按规定将托管银行承兑汇票办理出库." + &qu ...
- Get Start StrangeIOC for Unity3D
好久没有发blog了,因为只发原创内容,而去年发布的那几篇后来发现随便百度到处都是转载的或者各种网站自动扒的,我觉得既然大家都不尊重这种东西就没必要发上来了!不过由于工作原因最近在看Unity的一个I ...