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. Qt5+VS2010的安装及使用

    在我的博客<Win7下Qt5的安装及使用>中讲解了win7下Qt5+MinGW的安装及使用,本节再讲解win7下Qt5+VS2010的安装及使用.利用Qt5+MinGW开发应用程序比较麻烦 ...

  2. Delphi、Lazarus保留字、关键字详解

    Delphi.Lazarus保留字.关键字详解 来自橙子,万一的博客以及其他地方 保留字:变量等标识符可以再使用: 关键字:有特定含义,不能再次重新定义: 修饰字:类似保留字的功能,也就是说可以重用 ...

  3. C++ 的虚析构函数

    当一个基类的指针指向一个派生类的对象,并用该基类的指针去删除或者析构派生类对象时,如果基类的析构函数不是声明为虚函数,那么在析构时基类的析构函数将会被直接调用,派生类的析构函数应为没被调用而导致内存泄 ...

  4. 枚举之后define

    经常会看到类似下边的code写法,觉得这么写没什么意义. enum { AA, BB, CC, }; #define AA AA #define BB BB #define CC CC 尝试下边代码, ...

  5. Spring Environment(一)API 介绍

    Spring Environment(一)API 使用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 3. ...

  6. 记录点复习题目和linux学习

    哈希怎么底层.key放数组哪部分里面 HashMap实际上是一个“链表散列”的数据结构,即数组和链表的结合体. 开网页怎么获取页面 linux 看进程的cpu 和内存占用率 看哪个端口被占用     ...

  7. python+selenium—webdriver入门(一)

    一.浏览器最大化 二.设置浏览器分辨率大小 三.打印页面title 四.打印URL 五.控制浏览器前进或后退 #!/usr/bin/env python#-*- coding:utf-8 -*- fr ...

  8. 导入mysql报错问题

    今天数据导入报错:Got a packet bigger than‘max_allowed_packet’bytes的问题 2个解决方法: 1.临时修改:mysql>set global max ...

  9. Sprign中常用注解

    1.@Component 创建类对象,相当于配置<bean/> 2.@Service 与 @Component功能相同 2.1写在ServiceImpl类上 (建议在ServiceImpl ...

  10. 特级教师总结的教育之33条(ZZ)

    一位班主任发给家长的一则短信:“无论成绩好坏,请想想:每个孩子都是种子,只不过每个人的花期不同.有的花,一开始就灿烂绽放:有的花,需要漫长的等待.不要看着别人怒放了,自己的那棵还没有动静就着急,相信是 ...