[leetcode 120]triangle 空间O(n)算法
1 题目
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.
2 思路
这是一个动态规划题,每行的数据数对应行数,设F[m,n]表示到达第m行,第n列的最小代价,那么有
F[m,n]=min{F[i-1,j]+b,F[i-1,j-1]+b},其中b为第m行,第n列的数
那么边界值怎么处理呢?
我是设置正常值从1开始,0和最后一个值的后一位为IntMax。
这道题是我自己想出来的。
3 代码
①空间O(n^2),第一次想到的是这个
public int minimumTotal(List<List<Integer>> triangle){
int lineNumber = triangle.size();
Integer[][] F = new Integer[lineNumber][lineNumber+2];
//F(a,b) = min{F(a-1,b-1)+b,F(a-1,b)+b} F(a,b)表示 第a行第b个的最小代价
//set F(a,0) to MAX, F(a,B) to MAX , where B = triangle.get(a).size()+1, 0<=a<lineNumber;
//set the initial value
F[0][1] = triangle.get(0).get(0);
for (int i = 0; i < lineNumber; i++) {
F[i][0] = Integer.MAX_VALUE;
int lineSize = triangle.get(i).size() + 1;
F[i][lineSize] = Integer.MAX_VALUE;
}
// long former = 0;
//dynamic programming
for (int i = 1; i < lineNumber; i++) {
List<Integer> row = triangle.get(i);
int rowSize = row.size();
for (int j = 1; j <= rowSize; j++) {
F[i][j] = Math.min(F[i-1][j-1], F[i-1][j]) + row.get(j-1);
}
}
int min = F[lineNumber-1][1];
for (int i = 1; i <= lineNumber; i++) {
int temp = F[lineNumber-1][i];
if(temp < min){
min = temp;
}
}
return min;
}
②空间o(n),改进了一下
public int minimumTotal2(List<List<Integer>> triangle){
int lineNumber = triangle.size();
Integer[][] F = new Integer[2][lineNumber+2];
//F(a,b) = min{F(a-1,b-1),F(a-1,b)} + b; F(a,b)represent the minValue of the a row b list
//set the initial value and the boundary value
F[0][0] = Integer.MAX_VALUE;
F[0][1] = triangle.get(0).get(0);
F[0][2] = Integer.MAX_VALUE;
//dynamic programming
for (int i = 1; i < lineNumber; i++) {
List<Integer> row = triangle.get(i);
int rowSize = row.size();
for (int j = 1; j <= rowSize; j++) {
F[1][j] = Math.min(F[0][j-1], F[0][j]) + row.get(j-1);
}
F[1][0] = Integer.MAX_VALUE;
F[1][rowSize+1] = Integer.MAX_VALUE;
for (int j = 0; j <= rowSize+1; j++) {
F[0][j] = F[1][j];
}
}
int min = F[lineNumber > 1 ? 1 : 0][1];
for (int i = 1; i <= lineNumber; i++) {
int temp = F[lineNumber > 1 ? 1 : 0][i];
if(temp < min){
min = temp;
}
}
return min;
}
[leetcode 120]triangle 空间O(n)算法的更多相关文章
- LeetCode 120. Triangle (三角形)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode 120. Triangle (三角形最小路径和)详解
题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...
- LeetCode 120. Triangle三角形最小路径和 (C++)
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- LeetCode - 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- leetcode 120 Triangle ----- java
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode] 120. Triangle _Medium tag: Dynamic Programming
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Java for LeetCode 120 Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode] 120. Triangle (Medium)
原题 思路: dp,从下往上依次取得最小的,取到最上面的,就是一条最小的路径. class Solution { public: int minimumTotal(vector<vector&l ...
- [leetcode]120.Triangle三角矩阵从顶到底的最小路径和
Given a triangle, find the minimum path sum from top to bottom.Each step you may move to adjacent nu ...
随机推荐
- vim删除单词
参考资料: https://blog.csdn.net/grey_csdn/article/details/72355735 混迹于Windows.Linux以及Mac,选择加强自己的VIM水平应该不 ...
- 使用hMailServer搭建邮件服务器
本文没有什么高深的技术内容,只是使用hMailServer,介绍搭建邮件服务器的全过程,供参考. 一.安装邮件服务器组件 打开软件,点下一步 选择存储数据的数据库,这里有两种选择, 一种是使用嵌入型数 ...
- go语言练习
// main package main import ( "fmt" "runtime" "sync" ) func main() { f ...
- vue动态路由配置,vue路由传参
动态路由: 当我们很多个页面或者组件都要被很多次重复利用的时候,我们的路由都指向同一个组件,这时候从不同组件进入一个"共用"的组件,并且还要传参数,渲染不同的数据 这就要用到动态路 ...
- 带token的get和post方法
GET和POST传值失败,多半是传输的字符串和URL的事 public static string ExcuteGetToken(string serviceUrl, string ReqInfo, ...
- solr之创建core(搜索核心,包括索引和数据)的方法
我的solrhome为D:\solrHome\solr step1:进入solrHome会看到collection1文件夹,创建该文件夹的副本,重命名为product 进入product文件夹,进入d ...
- (转贴)fusionCharts属性参考API
一.FusionCharts的分类 关于FusionCharts的基本介绍我就不在这里浪费篇幅了,想了解的朋友自己去www.baigoogedu.com里面找吧.我就说说FusionCharts的官方 ...
- Android音频系统之AudioFlinger(三)
http://blog.csdn.net/xuesen_lin/article/details/8805091 1.1.1 PlaybackThread的循环主体 当一个PlaybackThread进 ...
- 透过摩拜和ofo,看产品从0到1时如何取舍需求(转)
大纲 背景介绍 从0至1,我们成功的关键是什么? 从0到1,我们为什么选择做?又为什么选择不做? 从0到1,我们面临什么选择?我们作出了什么选择? 从0到1,我们为什么作出了这种选择? 背景 在资本注 ...
- eclipse中tomcat启动设置参数
今天新接触一个项目,使用java+flex做的单机项目,由于模块太多,打成war后就有300M左右了.所以,启动的时候,比较慢,超过了e eclipse中默认的45s,当我进行修改启动事件后,有一 ...