【120-Triangle(三角形)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  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.

题目大意

  给定一个三角形,找出从顶究竟的最小路径和,每一步能够从上一行移动到下一行相邻的数字

解题思路

  递推方程:

  f(0,0)=a[0][0]

  f(i,0)=a[i][0]+f(i-1,0) (i>0)

  f(i,i)=a[i][i]+f(i-1,i-1)(i>0)

  f(i,j)=a[i][j]+MIN(f(i-1,j),f(i-1,j-1))(0

代码实现

算法实现类

import java.util.List;

public class Solution {

    public int minimumTotal(List<List<Integer>> triangle) {

        if (triangle == null || triangle.size() < 1) {
return 0;
}
// 创建数组的第二维度
int[][] minSum = new int[triangle.size()][]; // 创建数组的第一维度
for (int i = 0; i < minSum.length; i++) {
minSum[i] = new int[i + 1];
}
// 设置第一行
minSum[0][0] = triangle.get(0).get(0);
// 设置其他行
for (int i = 1; i < minSum.length; i++) {
List<Integer> line = triangle.get(i);
for (int j = 0; j < minSum[i].length; j++) {
if (j == 0) {
minSum[i][0] = line.get(0) + minSum[i - 1][0];
} else if (i == j) {
minSum[i][j] = line.get(j) + minSum[i - 1][j - 1];
} else if (j < i) {
minSum[i][j] = line.get(j) + Math.min(minSum[i - 1][j], minSum[i - 1][j - 1]);
}
}
}
//找最后一行的最小值就是所求的解
int min = minSum[minSum.length - 1][0];
int length = minSum[minSum.length - 1].length;
for (int i = 1; i < length; i++) {
if (min > minSum[length - 1][i]) {
min = minSum[length - 1][i];
}
} return min;
}
}

评測结果

  点击图片。鼠标不释放。拖动一段位置。释放后在新的窗体中查看完整图片。

特别说明

欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47651229

【LeetCode-面试算法经典-Java实现】【120-Triangle(三角形)】的更多相关文章

  1. 【LeetCode-面试算法经典-Java实现】【118-Pascal&#39;s Triangle(帕斯卡三角形)】

    [118-Pascal's Triangle(帕斯卡三角形(杨辉三角))] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given numRows, generate ...

  2. 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

    [139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...

  3. 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】

    [053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...

  4. 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】

    [062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...

  5. 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

    [059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...

  6. 【LeetCode-面试算法经典-Java实现】【136-Single Number(仅仅出现一次的数字)】

    [136-Single Number(仅仅出现一次的数字)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array of integers, ev ...

  7. 【LeetCode-面试算法经典-Java实现】【075-Sort Colors (颜色排序)】

    [075-Sort Colors (颜色排序)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array with n objects colore ...

  8. 【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】

    [101-Symmetric Tree(对称树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, check whether ...

  9. 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】

    [109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 ...

随机推荐

  1. ijkplayer视频播放

      http://android-doc.com/androiddocs/2017/1018/5416.html https://www.2cto.com/kf/201801/714366.html ...

  2. android的低内存管理器【转】

    本文转载自:http://blog.csdn.net/haitaoliang/article/details/22092321 版权声明:本文为博主原创文章,未经博主允许不得转载. 安卓应用不用太在意 ...

  3. php连接符

    php连接符 很多时候我们需要将几个字符串连接起来显示,在PHP中,字符串之间使用 “点” 来连接,也就是英文中的半角句号 " . " ." . " 是字符串连 ...

  4. spark rdd median 中位数求解

    lookup(key) Return the list of values in the RDD for key key. This operation is done efficiently if ...

  5. 产生冠军--hdoj

    产生冠军 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  6. webpack到底怎么用?

    webpack到底怎么用? https://www.zhihu.com/question/39290543

  7. systemd实践: 依据情况自动重启服务

    systemd服务异常自动重启很好用,但有的时候希望某些服务只在特定情况下进行重启,其他时候不要自动重启(比如OOM,需要人工介入). 本文抛砖引玉,旨在能够让读者对systemd的重启机制有一定了解 ...

  8. if,elif,else的关系 input print int的用法

    qian=input("找劳保网是什么网站?:")if qian=="zhaolaobaowang.com": print("正确")els ...

  9. Triangle 1.6 (A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator)

    Triangle 一个二维高质量网格(mesh)生成器和Delaunay三角化工具. PSLG(Planar Straight Line Graph)约束Delaunay三角网(CDT)与Delaun ...

  10. python 飞机大战 实例

    飞机大战 #coding=utf-8 import pygame from pygame.locals import * import time import random class Base(ob ...