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 ...
随机推荐
- javascript遍历Json对象个数
var data={}; for (var d in data) { $(data[d]).each(function (i, e) { aler ...
- JS 实现 startWith endWith函数
String.prototype.startWith = function(s) { if (s == null || s == "" || this.length == 0 || ...
- jsp验证码 (通过单击验证码或超链接换验证码)
#code.jsp <%@ page language="java" import="java.util.*" import="java.awt ...
- dp优化
入口 A(fzu 1894) 普通的单调队列,trick是进队判断的符号选取(>=wa , >ac). B(poj 2823) 没什么好说的 ,坑爹poj g++,tle ;c++,ac. ...
- 关于Redis
在同步dump.rdb文件时要执行service redis stop后,再拷贝目标rdb文件过去,然后再start 而不是拷贝目标rdb文件过去后直接执行restart 因为redis在执行sto ...
- Direct3D 索引缓存
小学的时候我们知道3个顶点组成一个三角形,那么四个顶点我们会说有4个三角形.这就是一个顶点同时参与了四次绘制三角形的结果. 在程序中也一样,比如我们绘制的两个三角形是挨着一起的,总有几个顶点是重合的. ...
- 链表list容器中通过splice合并链表与merge的不同,及需要注意的问题
#include "stdafx.h" #include <iostream> #include <list> #include <algorithm ...
- NFinal ajax
AJAX返回 我们经常会用到ajax,当我们想返回json时,需要这样写. context.Response.ContentType="application/json"; con ...
- IE iframe 中 js 的 cookie 读写不到的解决办法
1.看这里(改服务器配置) http://www.cr173.com/html/16696_1.html 2.使用object模拟iframe,不使用iframe框架 <html> < ...
- Sqlserver 系列(一):常用函数
(1)聚合函数 sum,max,min,avg,count (2)日期函数 datediff ,dateadd, datepart,getdate,month,day (3)字符串函数 ltrim,r ...