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 ...
随机推荐
- SQL省市区三级表结构
-- 表的结构 areaDROP TABLE area;CREATE TABLE area ( id int NOT NULL , areaID int NOT NULL, area va ...
- if条件语句练习(相亲)
public class a { public static void main(String[] arg){ //F是房子,Q是钱,N是能力,等于0说明没有,等于1说明有: 有一项具备则可以嫁,都不 ...
- Android平台的事件处理机制和手指滑动例子
Android平台的事件处理机制有两种 基于回调机制的事件处理:Android平台中,每个View都有自己的处理事件的回调方法,开发人员可以通过重写View中的这些回调方法来实现需要的响应事件. 基于 ...
- python3-day1(基础总结)
总结比较抽象的难点: 1.二进制运算 60 & 13 =12 60 | 13 =61 60 ^ 13 =49 60<<2 =240 60>>2 =15 ...
- Lua Interface基础使用
Lua是一种可爱的脚本语言,由Roberto Ierusalimschy.Waldemar Celes 和 Luiz Henrique de Figueiredo所组成并于1993年开发. 其设计目的 ...
- java删除文件夹下所有文件
package org.sw; import java.io.File; /** * * @author mengzw * @since 3.0 2014-2-26 */ public class D ...
- 男同胞爱小秘籍--作为爱他的女朋友了几天C规划
各位男同胞,不知道你的女朋友没有在过去的一问天,你这个问题~~ 场景重现: 女友:"今天天气不错." 你们:"对" 女友:"今天是我们知道它的最初几天 ...
- LR选择哪种方式录制
LR选择哪种方式录制,有以下考虑原则: 1.基于浏览器的应用程序推荐使用HTML-basic script方式录制 2.不是基于浏览器的应用程序推荐使用URL-basic script方式录制 3.如 ...
- hbase权威指南学习笔记--架构--存储
HBase主要处理两种文件:预写日志(Write-Ahead Log,WAL),实际的数据文件. 一个基本的流程是客户端首先联系ZooKeeper子集群查找行健数据所在的region服务器名.(通过Z ...
- Ubuntu13.04手动安装nvidia显卡驱动
1. 下载最新版的nVidia驱动,命名为NVIDIA.run. http://www.nvidia.com/page/drivers.html 2.编辑blacklist.conf. sudo ge ...