LeetCode-Triangle[dp]
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[i,j]表示从位置(i,j)出发到达三角形底部的最小路径和,所以状态转移方程有:
f[i,j]=min(f[i+1,j],f[i+1,j+1]) (i<size-1&&i>=0,j<i+1);
参考代码:
public class Solution {
public static int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
int trianlgesize=triangle.size();
int dp[][]=new int[trianlgesize][trianlgesize];
for(int i=0;i<triangle.get(trianlgesize-1).size();i++){
dp[trianlgesize-1][i]=triangle.get(trianlgesize).get(i);
}
for(int i=trianlgesize-2;i>=0;i--){
for(int j=0;j<i+1;j++){
dp[i][j]=Math.min(dp[i+1][j], dp[i+1][j+1])+triangle.get(i).get(j);
}
}
return dp[0][0];
}
}
LeetCode-Triangle[dp]的更多相关文章
- [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 distinct-subsequences(DP)
参考https://oj.leetcode.com/problems/distinct-subsequences 动态规划方程 dp[i][j]=dp[i-1][j-1]+dp[i-1][j] (s( ...
- leetcode — triangle
/** * Source : https://oj.leetcode.com/problems/triangle/ * * * Given a triangle, find the minimum p ...
- LeetCode: Triangle 解题报告
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- POJ 1163 The Triangle DP题解
寻找路径,动态规划法题解. 本题和Leetcode的triangle题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...
- Triangle(dp)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- nyoj--18--The Triangle(dp水题)
The Triangle 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure ...
- poj 1163 The Triangle(dp)
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43993 Accepted: 26553 De ...
- LeetCode - Triangle
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
随机推荐
- github pages部署静态网页
如果你的项目只是一个静态网站,就没有必要再去整什么服务器,github pages 提供了搭建静态网站的功能: 为什么使用Github Pages 1. 搭建简单而且免费: 2. 支持静态脚本: 3. ...
- (转)导出EXCEL时科学计数法问题
//1) 文本:vnd.ms-excel.numberformat:@ //2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd //3) 数字:vnd.ms-e ...
- 可视化之Earth NullSchool
上两篇我们分别介绍了<Berkeley Earth>和<AQICN>两个网站,今天来看一下Earth NullSchool. 这个网站的特色是风向图,之前有一篇可视化之风向图, ...
- WPF中带水印的Textbox
很多时候我们都希望通过水印来告诉用户这里该填什么样格式的数据,那么我们就希望有这样的一个控件. 为了方便起见,先定义一个依赖属性专门来存放水印中显示的字符串. public sealed class ...
- module.exports,exports,export和export default,import与require区别与联系【原创】
还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: modu ...
- IT学习网站
网站 网站定位 http://www.51cto.com/ 中国领先的IT技术网站. http://www.iteye.com/ 内容齐全,功能丰富的中文IT技术门户和社区网站. http://www ...
- 使用Iterator的方式也可以顺利删除和遍历
使用Iterator的方式也可以顺利删除和遍历 eg: public void iteratorRemove() { List<Student> students = this.getSt ...
- php 函数形参前面加上&
<?php function test(&$a){ $a=$a+100; } $b=1; echo $b;//输出1 test($b);//这里$b传递给函数的其实是$b的变量内容所处的 ...
- 使用dns批量管理普通主机名相关问题
1.dns配置 日常管理主机过程中,会有很多地方需要使用到主机名的,当主机非常多的时候,就不适合使用hosts来管理和同步的所有主机hosts了,这个时候就可以使用dns来管理主机名映射和变动 dns ...
- js或者jq的tab切换
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...