博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode--Triangle
阅读量:5134 次
发布时间:2019-06-13

本文共 3006 字,大约阅读时间需要 10 分钟。

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

Note:

Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

public class Solution {    /**Top-down method. Since only O(n) extra memory is allowed, we use a deque to save the intermediate result.     * @author Averill Zheng     * @version 2014-06-23     * @since JDK 1.7     */    public int minimumTotal(List
> triangle) { int min = Integer.MAX_VALUE; int length = triangle.size(); if(length == 0) return 0; else if(length == 1) return triangle.get(0).get(0); else { Queue
sums = new LinkedList
(); sums.add(triangle.get(0).get(0)); for(int i = 1; i < length; ++i){ List
aList = triangle.get(i); int equalIndex = sums.poll(); sums.add(equalIndex + aList.get(i)); int smallerIndex = (i > 1) ? sums.peek(): Math.abs(equalIndex); for(int j = i - 1; j > 0; --j){ sums.add(Math.min(smallerIndex + aList.get(j), equalIndex + aList.get(j))); equalIndex = sums.poll(); smallerIndex = sums.peek(); } sums.add(equalIndex + aList.get(0)); } while(sums.peek() != null){ min = Math.min(min, sums.poll()); } } return min; }}

 

method 2: bottom-up method

public class Solution {    public int minimumTotal(List
> triangle) { Queue
sums = new LinkedList
(); int length = triangle.size(); if(length == 0) return 0; else if(length == 1) return triangle.get(0).get(0); else { List
lastList = triangle.get(length - 1); for(int i = 0; i < length; ++i) sums.add(lastList.get(i)); for(int i = length - 2; i > -1; --i){ List
currentList = triangle.get(i); for(int j = 0; j < i + 1; ++j){ int equalIndex = sums.poll(); int largerIndex = sums.peek(); sums.add(currentList.get(j) + Math.min(equalIndex, largerIndex)); } sums.poll(); } } return sums.peek(); }}

 

method 3. bottom-up , and use List<Integer> as the data structure to save the intermediate result.

public class Solution {    public int minimumTotal(List
> triangle) { int length = triangle.size(); List
pathSum = new ArrayList
(); if(length == 0) return 0; pathSum.addAll(triangle.get(length - 1)); for(int i = length - 2; i > -1; --i){ List
aList = triangle.get(i); for(int j = 0; j < i + 1; ++j) pathSum.set(j, Math.min(pathSum.get(j), pathSum.get(j + 1))+ aList.get(j)); } return pathSum.get(0); }}

  

转载于:https://www.cnblogs.com/averillzheng/p/3805657.html

你可能感兴趣的文章
[Swift]LeetCode85. 最大矩形 | Maximal Rectangle
查看>>
ECMAScript 6 + Babel
查看>>
[AngularJS NG-redux] Handle Asynchronous Operations with Middleware
查看>>
[React] Compound Component (React.Children.map & React.cloneElement)
查看>>
JavaWeb开发必会技巧1——导入jar包
查看>>
一幅图看懂prototype与[[Prototype]]
查看>>
读《JavaScript权威指南》笔记(三)--对象
查看>>
AI ResNet V1
查看>>
BytePS
查看>>
UNIX环境高级编程 第11章 线程
查看>>
匿存函数,内存函数,递归函数,二分法查找
查看>>
MySQL的show profile(已过时)简介以及该功能在MySQL 5.7中performance_schema中的替代
查看>>
2014年12月21号面试
查看>>
easyui 自定义editor扩展 propertygrid
查看>>
SQL SERVER-Extendevent检测TempDB增长
查看>>
vuex的简单教程
查看>>
Maximum Gap
查看>>
sublime3
查看>>
我学Linq/1
查看>>
UIView Methods
查看>>