lintcode-easy-Triangle
2016-03-09 08:30
429 查看
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
Given the following triangle:
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
动态规划:
创建一个和最后一行一样大小的int数组,并赋值成和最后一行一样
从倒数第二行开始往回数,把数组里的值更新成(当前这个元素)和(下一行相邻两个数中较小的数)的和
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).
动态规划:
创建一个和最后一行一样大小的int数组,并赋值成和最后一行一样
从倒数第二行开始往回数,把数组里的值更新成(当前这个元素)和(下一行相邻两个数中较小的数)的和
public class Solution { /** * @param triangle: a list of lists of integers. * @return: An integer, minimum path sum. */ public int minimumTotal(int[][] triangle) { // write your code here if(triangle == null || triangle.length == 0) return 0; int size = triangle.length; int[] result = new int[size]; for(int i = 0; i < size; i++){ result[i] = triangle[size - 1][i]; } for(int i = size - 2; i >= 0; i--){ for(int j = 0; j <= i; j++){ result[j] = triangle[i][j] + Math.min(result[j], result[j + 1]); } } return result[0]; } }
相关文章推荐
- flume简介
- 亿同科技实习第一天
- POJ 1322 Chocolate(概率DP)
- Android 自动拨打电话
- 第一次做项目的总结
- 哈弗曼编码解码
- 微信公众号通过链接实现关注(盗用广告主流量主)
- HTTP中Get与Post的区别
- Thread/Runnable
- 【BZOJ4419】[Shoi2013]发微博【set】
- bzoj4006 管道连接 斯坦纳森林 子集Dp
- RDLC 微软报表 导出Excel时产生多个工作表 (worksheet)
- vsftpd:500 OOPS: vsftpd: refusing to run with writable root inside chroot ()错误的解决方法
- 一张图让你迅速理解区分各种开源许可证GPL、BSD、MIT、Mozilla、Apache、LGPL
- iOS之微信支付(二)——本地生成订单集成详情
- 【Android】常见问题解答
- 谋哥:App自推广这个概念就由我来创立了!
- Difference between MBR and GPT in hard disk
- Find K most Frequent items in array
- 主子窗口填报