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)算法的更多相关文章

  1. LeetCode 120. Triangle (三角形)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  2. LeetCode 120. Triangle (三角形最小路径和)详解

    题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...

  3. LeetCode 120. Triangle三角形最小路径和 (C++)

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  4. LeetCode - 120. Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. leetcode 120 Triangle ----- java

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  6. [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 ...

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

  8. [leetcode] 120. Triangle (Medium)

    原题 思路: dp,从下往上依次取得最小的,取到最上面的,就是一条最小的路径. class Solution { public: int minimumTotal(vector<vector&l ...

  9. [leetcode]120.Triangle三角矩阵从顶到底的最小路径和

    Given a triangle, find the minimum path sum from top to bottom.Each step you may move to adjacent nu ...

随机推荐

  1. Python 常用模块之re 正则表达式的使用

    re模块用来使用正则表达式.正则表达式用来对字符串进行搜索的工作.我们最应该掌握正则表达式的查询,更改,删除的功能.特别是做爬虫的时候,re模块就显得格外重要. 1.查询 import re a = ...

  2. Https如何确保传输安全的

    1.对称加密算法 加密和解密算法是公开的,那个密钥是保密的, 只有双方才知道, 这样生成的加密消息(密文) 别人就无法得知了. 2.非对称加密算法 RSA算法非常有意思,是有一对儿钥匙, 一个是保密的 ...

  3. option_match

    //与match区别:如果没有匹配到,返回NULLCREATE (olive:Person {name:'Olive Stone'}),(charlie:Person {name:'Charlie S ...

  4. java的nio例子

    package main; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.Inet ...

  5. OneZero第三周第五次站立会议(2016.4.8)

    1. 时间: 15:10--15:25  共计15分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http ...

  6. XML 解析的两种方法

    申请博客有一段时间了,一直没有写些什么,今天写一下被遗忘的 xml,因为 ios 现在一般都用 JSON,但毕竟还有一部分老一些的服务器还会有 xml xml 格式的解析方式有两种 1.SAX解析: ...

  7. JavaScript的基础篇

    一.JavaScript的引入方式 1)js的引用方式 <body> <!--引入方式一,直接编写--> <script> alert("hello wo ...

  8. python中的特殊成员

    python中的特殊成员: 小甲鱼论坛总结

  9. java.security.cert.CertificateException: No subject alternative names matching IP address xxx.xxx.xxx.xxx found

    https与http不同的是,https加密,需要验证证书,而http不需要. 在连接的代码中加上: static { disableSslVerification(); } private stat ...

  10. C++程序生成.exe文件,在文件夹中运行时闪现问题

    问题描述:在IDE(此为Dev-C++)中编写C++程序,运行时会产生如下文字 但我想取消这三行的显示. 解决方法:1:在IDE中运行时,“请按任意键继续”是消失不掉的,但在该程序的保存路径下可以消灭 ...