/**
* Source : https://oj.leetcode.com/problems/triangle/
*
*
* 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.
*
*/
public class Triangle { /**
* 求出三角形中和最小的路径
* 使用常数空间O(n)
* 从最后一层开始计算,第i层的个数为i个,每个元素和第i+1层的左右下角两个元素中较小的一个进行求和作为该位置新的元素
*
*
* @param triangle
* @return
*/
public int getMinimumPath (int[][] triangle) {
if (triangle.length == 0) {
return 0;
}
int m = triangle.length;
int n = triangle[0].length;
int[] result = triangle[triangle.length-1];
for (int i = m-2; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
result[j] = Math.min(result[j], result[j+1]) + triangle[i][j];
}
}
return result[0];
} public static void main(String[] args) {
Triangle triangle = new Triangle();
int[][] arr = new int[][]{
{2},
{3,4},
{6,5,7},
{4,1,8,3}
}; System.out.println(triangle.getMinimumPath(arr));
}
}

leetcode — triangle的更多相关文章

  1. [LeetCode] Triangle 三角形

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

  2. [leetcode]Triangle @ Python

    原题地址:https://oj.leetcode.com/problems/triangle/ 题意: Given a triangle, find the minimum path sum from ...

  3. LeetCode - Triangle

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

  4. LeetCode -- Triangle 路径求最小和( 动态规划问题)

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

  5. leetcode Triangle及其思考

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

  6. LeetCode Triangle 三角形(最短路)

    题意:给一个用序列堆成的三角形,第n层的元素个数为n,从顶往下,每个元素可以选择与自己最近的两个下层元素往下走,类似一棵二叉树,求最短路. [], [,4], [6,,7], [4,,8,3] 注意: ...

  7. leetcode—triangle

    1.题目描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adj ...

  8. LeetCode: Triangle 解题报告

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

  9. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

随机推荐

  1. BJOI2019Day1 数据&标程&题解

    链接: https://pan.baidu.com/s/16L5GHvo9WtY20sZoqjuQNQ 提取码: 3iur

  2. PHP调用微博接口实现微博登录的方法示例

    在平时项目开发过程中,除了注册本网站账号进行登录之外,还可以调用第三方接口进行登录网站.这里以微博登录为例.微博登录包括身份认证.用户关系以及内容传播.允许用户使用微博帐号登录访问第三方网站,分享内容 ...

  3. 利用jquery-barcode.js实现生成条形码

    jquery-barcode官网 js下载地址-github 代码示范(官网上也有) <!DOCTYPE html> <html> <head> <meta ...

  4. 20181117-python第二章学习小结-part2

    浮点型补充: 有限小数与无限循环小数,不包括无理数! 小数点后面的数据运算太复杂,精确度不及整数! 尽量使用科学计数表示小数 列表学习(语法) 创建:[] list = []  #创建空表 list ...

  5. On the Optimal Approach of Survivable Virtual Network Embedding in Virtualized SDN

    Introduction and related work 云数据中心对于虚拟技术是理想的创新地方. 可生存性虚拟网络映射(surviavable virtual network embedding ...

  6. MicroSoft CryptoAPI data/file encrypt/decrypt

    linux 用第三方库 Crypto++, 还未实战. CryptoAPI使用两种密钥:会话密钥与公共/私人密钥对.会话密钥使用相同的加密和解密密钥,这种算法较快,但必须保证密钥的安全传递.公共/私人 ...

  7. 动态sql语句,非存储过程,如何判断某条数据是否存在,如果不存在就添加一条

    已知一个表 table 里面有两个字段  A1 和 A2 如何用动态语句 判断 A1 = A , A2=B 的数据是否存在,如果不存在,就添加一条数据, A1 = A , A2 = B INSERT  ...

  8. 显存充足,但是却出现CUDA error:out of memory错误

    之前一开始以为是cuda和cudnn安装错误导致的,所以重装了,但是后来发现重装也出错了. 后来重装后的用了一会也出现了问题.确定其实是Tensorflow和pytorch冲突导致的,因为我发现当我同 ...

  9. linux下SS 网络命令详解

    ss命令用来显示处于活动状态的套接字信息. ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容. 但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信息,而且比n ...

  10. Lesson 26 The best art critics

    Text I am an art student and I paint a lot of pictures. Manay people pretend that they understand mo ...