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. 用VS2010打开VS2012项目

    1.修改解决方案文件,即.sln文件: 用记事本打开.sln文件,把其中的 Microsoft Visual Studio Solution File, Format Version 12.00 # ...

  2. GBDT-梯度提升树

    随机森林:bagging思想,可以并行,训练集权值相同 可以是分类树,回归树 输出结果(分类树):多数投票         (回归树):均值 减少方差 对异常数据不敏感 GBDT:拟合损失函数 boo ...

  3. 多个tomcat shutdown.sh 导致无法正常关闭的问题

    1. 今天启动两个tomcat , 但是由于个人失误,只改了以下两个端口 ,忘记修改shutdown相应端口.这是启动两个tomcat ,可以正常启动并访问.. <Connector port= ...

  4. AX_Args

    Args args; FormRun formRun; ; args = new Args(); args.name(formstr(FormName)); args.caller(); args.r ...

  5. python中的open( )函数

    函数原型 open(file, mode=‘r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True) buff ...

  6. MySQL数据库innodb_rollback_on_timeout默认值的危害?

    http://www.ywnds.com/?p=9560 一.innodb_rollback_on_timeout变量 有时侯会发生事务超时的情况,MySQL会返回类似这样的错误:   1 ERROR ...

  7. 26、xcode8.0 解决没有iPhone4模拟器问题

    第一步:随便打开Xcode项目 ,点击电脑屏幕右上角 Xcode->Preference 第二步: 点击下载ios 8.1 Simulator 等到下载完成即可在xcode中添加iphone4s ...

  8. php接口 接受ios或android端图片; php接收NSData数据

    备注下, [自己无意中用的方法]接收时,设置两个参数,一个是图片名称,另一个是实际文件.然后用$_FILES处理就可以了. 如: $icon = $_POST['icon'];//这个是图片的名称,用 ...

  9. idea intellij对Spring进行单元测试

    1.加入Junit4及SpringJUnit4支持 <!-- junit --> <dependency> <groupId>junit</groupId&g ...

  10. python读取文件操作.CSV

    #-*- encoding:utf-8 -*- import numpy as np import pandas as pd def test(): # header=0,表示文件第0行为列索引 # ...