LeetCode -- Tiangle
Question:
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).
Analysis:
给出一个三角形,找到从顶部到底部的最小路径。每次跨行移动时只能在相邻的元素间移动。示例如上。
显然用DP,初始条件dp[0][0] = 顶部第一个元素。
状态转移方程为:
dp[i][j] = min(dp[i-1][j-1], dp[i-1][j]) + triangle[i][j];
当然要注意每行的最后一个元素和第一个元素,只有一个来源。
最后再找出最后一行中最小的数值即可。
本来是很简单的,但是由于是List取数时没有数组方便,因此调bug花了一些时间。。。
代码如下:
public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if(triangle == null || triangle.size() == 0)
return 0;
if(triangle.size() == 1)
return triangle.get(0).get(0);
List<ArrayList<Integer>> dp = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> dp0 = new ArrayList<Integer>();
dp0.add(triangle.get(0).get(0));
dp.add(dp0);
int row = triangle.size();
for(int i=1; i<row; i++ ) {
ArrayList<Integer> dpi = dp.get(i-1);
List<Integer> tempi = triangle.get(i);
ArrayList<Integer> dpii = new ArrayList<Integer>();
int col = tempi.size();
for(int j=0; j<col; j++) {
if(j == 0)
dpii.add(dpi.get(j)+tempi.get(0));
else if(j < dpi.size()){
dpii.add(Math.min(dpi.get(j-1), dpi.get(j)) + tempi.get(j));
}
else
dpii.add(dpi.get(j-1) + tempi.get(j));
}
dp.add(dpii);
}
int min = Integer.MAX_VALUE;
dp0.clear();
dp0 = dp.get(dp.size()-1);
for(int i=0; i<dp0.size(); i++)
if(min > dp0.get(i))
min = dp0.get(i);
return min;
}
}
LeetCode -- Tiangle的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- vue项目中缓存问题
单页面应用总是存在缓存问题,特别是在微信端,更新页面之后访问的还是老页面,缓存的问题是因为用户访问的脚本地址并没有改变,浏览器就会读取原来的脚本 网上有几种解决办法,首先列举一下 1.加meta,禁止 ...
- ctf题目writeup(4)
2019.1.31 题目:这次都是web的了...(自己只略接触隐写杂项web这些简单的东西...) 题目地址:https://www.ichunqiu.com/battalion 1. 打开链接: ...
- R语言学习笔记(三):零碎知识点(1-10)
1--c() c表示"连接"(concatenate). 在R中向量是连续存储的,因此不能插入或删除元素. 2--seq() seq()的特殊用法,可以用在for循环里for(i ...
- Windows Server 2012下手动配置IIS的文件夹访问权限
当新建一个website的时候,一般情况下IIS对相应的物理文件夹的访问权限是不够的. 针对匿名认证(anonymous authentication)需要: 打开文件夹properties-> ...
- python2.7练习小例子(十六)
16):题目:输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. 程序分析:利用 while 或 for 语句,条件为输入的字符不为 '\n'. 程序源代码: #!/u ...
- 使用java多线程分批处理数据工具类
最近由于业务需要,数据量比较大,需要使用多线程来分批处理,提高处理效率和能力,于是就写了一个通用的多线程处理工具,只需要实现自己的业务逻辑就可以正常使用,现在记录一下 主要是针对大数据量list,将l ...
- 高德API+.NET解决租房问题(新增诚信房源)
作者:李国宝链接:https://zhuanlan.zhihu.com/p/22105008(欢迎点赞)来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 之前有小伙伴反应 ...
- IDLE激活方法
激活流程 一.通过Activation code 方式激活, 注册码获取地址为:http://idea.lanyus.com/ 在idea或者pycharm的Activation code中输入 注册 ...
- allegro导入网表过程中出现的错误信息
1. 找不到焊盘PAD,下面这句话的意思是器件封装找不到焊盘46.pad WARNING(SPMHNI-): Unable to load symbol ): Could not find padst ...
- springmvc常用jar包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans ...