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 ...
随机推荐
- win8安装sql2008及设置登陆名问题
1. .net3.5安装 使用win8系统自带的升级功能无法成功安装.其实Windows8安装文件中已经集了.Net3.5, (1)此时只需要使用虚拟光驱加载Windows8 ...
- Overfitting&Underfitting Problems
这次根据结合Google的翻译果然速度快上许多,暂时休息,晚上在传一个exm2的随笔. 关于过度拟合下的问题 考虑从x∈R预测y的问题,下面的最左边的图显示了将\(y=\theta_0+\theta_ ...
- ecshop 商品分类页 取得当前分类下的子分类方法
ecshop的商品分类页面category.php 下的分类,默认是取得所有同级父分类以及父类别的子分类.比如,我点击进入是A商品分类的页面 category.php?id=1,事实上 我只需要取得父 ...
- Ubuntu16.04 + caffe-ssd + [CPU_ONLY] + KITTI 训练总结
本次训练主要参考:http://blog.csdn.net/jesse_mx/article/details/65634482 感谢 Jesse_Mx ,帮助了我很多. 坑一[openCV未安装成功] ...
- sql with as 用法(转载)
一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候, ...
- JAVA类型擦除
Java泛型-类型擦除 一.概述 Java泛型在使用过程有诸多的问题,如不存在List<String>.class, List<Integer>不能赋值给List<Num ...
- 2017全球互联网技术大会回顾(附PPT)
有幸遇见 GITC2017上海站,刚好遇见你! 为期两天(6.23~24)的GITC大会在上海举行,我有幸参加了24号的那场,也就是上周六,之所以今天才来回顾,是我想等PPT出来后分享给大家! 这应该 ...
- [leetcode-496-Next Greater Element I]
You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of n ...
- 7.java的请求转发和请求重定向
1.请求重定向:是客户端的行为,response.sendRedirect(),从本质上讲等同于两次请求,前一次的请求对象不会保存,地址栏的URL地址会改变,一次新的转发. 2.请求转发:是服务器的行 ...
- canvas学习总结五:线段的端点与连接点
我们在第三节中描述了线段的绘制,其中线段的属性lineWidth是用来改变线段的宽度.让我们来回忆下线宽的用法 function drawLine(){ cxt.lineWidth = 3; cxt. ...