leetcode — triangle
/**
* 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的更多相关文章
- [LeetCode] Triangle 三角形
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode]Triangle @ Python
原题地址:https://oj.leetcode.com/problems/triangle/ 题意: Given a triangle, find the minimum path sum from ...
- LeetCode - Triangle
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- LeetCode -- Triangle 路径求最小和( 动态规划问题)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- leetcode Triangle及其思考
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode Triangle 三角形(最短路)
题意:给一个用序列堆成的三角形,第n层的元素个数为n,从顶往下,每个元素可以选择与自己最近的两个下层元素往下走,类似一棵二叉树,求最短路. [], [,4], [6,,7], [4,,8,3] 注意: ...
- leetcode—triangle
1.题目描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adj ...
- LeetCode: Triangle 解题报告
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- CF.802C.Heidi and Library (hard) (费用流zkw)
题目链接 复习了下餐巾计划问题.全忘了=-= 首先这是一道网络流.然后本题有\(n\)种建图方法,以及\(smy\) dalao还有单纯形做法. 先假设所有物品都是买入的.那么对于每一天,拆成两个点\ ...
- 雕刻机制作 PCB 指南
之前使用过感光蓝油制作过 PCB,虽然感光法精度高,但个人制作耗时耗力,发给厂家周期又很长.看到国外的网友使用雕刻机制作 PCB 视频之后.几番周折之后还是成功了.有感于网上几乎没有一份完整的雕刻机 ...
- 51nod1007-正整数分组(dp)
题目 :http://www.51nod.com/Challenge/Problem.html#!#problemId=1007 大意就是给一堆正整数,分成和最接近的两组. 最开始没什么想法,2^n尝 ...
- 一个陌生女人的来信 Brief einer Unbekannten
[Österreich] Stefan Zweig Mein Kind ist gestorben, unser Kind - jetzt habe ich niemanden mehr in der ...
- sudo命令详解
语法 sudo(选项)(参数) 选项 选项 说明 -b 在后台执行指令: -h 显示帮助: -H 将HOME环境变量设为新身份的HOME环境变量: -k 结束密码的有效期限,也就是下次再执行sudo时 ...
- 从零开始构建一个centos+jdk7+tomcat7的docker镜像文件
从零开始构建一个centos+jdk7+tomcat7的镜像文件 centos7系统下docker运行环境的搭建 准备centos基础镜像 docker pull centos 或者直接下载我准备好的 ...
- 用户注册之后,通过网易邮箱服务器(smtp.163.com)发送电子邮箱到注册者邮箱的的确认通知短信.(可根据需求自行调整)
Member 是数据实体,穿过来的也就是当前注册用户的信息. 存储的数据一定要有邮箱信息 private void SendAuthCodeToMember(Member member) ...
- 20175324 mycp
具体描述: 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.bin 用来把文本文件(内容 ...
- .htaccess 文件 访问二级域名 对应的 指定文件夹
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / # 绑定m.xxx.cc 到子目录m RewriteCond %{HTTP_ ...
- 将DataRow拷贝到另一个DataRow
DataRow dr = dtPadFluid.Rows[gvPadFluid.FocusedRowHandle]; foreach (DataColumn dc in _dr.Table.Colum ...