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,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- hcna(华为)_Telnet篇
Telnet提供了一个交互式操作界面,允许终端远程登录到任何可以充当 Telnet服务器的设备.Telnet用户可以像通过Console口本地登录一样对 设备进行操作.远端Telnet服务器和终端之间 ...
- Hashlib加密,内置函数,安装操作数据库
hashlib模块的md5加密: md5同样的内容加密后是一样的 md5加密后是不可逆的.即能加密,没办法解密. 撞库: 只是针对简单的,因为同样的内容加密后是一样的,难一点就不行了. 登录密码:注册 ...
- BZOJ 3864
dp of dp 我就是来贴个代码 #include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=(a ...
- c# asp.net mvc使用斑马GK888t打印机打印标签
前言 c#语言,asp.net mvc,南京都昌电子病历模板工具(类似word),斑马GK888t,打印手腕带和标签纸. 实现步骤为:在页面上显示一个或多个都昌模板工具,点击页面上的button,出现 ...
- 多阶段构建Docker镜像
在Docker 17.05及更高的版本中支持支持一种全新的构建镜像模式:多阶段构建: 多阶段构建Docker镜像的最大好处是使构建出来的镜像变得更小: 目前常见的两个构建镜像的方式为: 1.直接使用某 ...
- mac上安装iterm2的一些步骤记录
1.首先到item官网上下载item 下载地址 http://iterm2.com/ 2.把iitem2设置为默认终端: 3.设置快速打开关闭的hotkey 我们这里设置为command + T键 ...
- angular.uppercase()
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- centos7 ping不通 name or service not known
最近打算为centos安装一个界面时,发现不能上网.ping www.baidu.com 报name or service not known. 原来网络配置没设好. 一.选择VMWare的NAT模式 ...
- Markdown基础语法笔记
# 一级标题## 二级标题### 三级标题###### #号之后记得加一个空格 仅支持1-6级标题 ### 列表 - 文本1 - 文本2 - 文本3+ 列表2* 列表2 ### 有序列表1. 有序文 ...
- ECharts使用:this.dom.getContext is not a function
echarts 画图报错 this.dom.getContext is not a function; 原因:因为在初始化echarts的时候,echarts.js规定只能使用dom原生方法获取标签, ...