LeetCode Triangle
来源:互联网 发布:spss数据分析案例 编辑:程序博客网 时间:2024/06/10 00:46
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.
思路:数塔的DP思想,为了不妨碍后面的计算,我们每行计算从后面开始。
public class Solution { public int minimumTotal(List<List<Integer>> triangle) { int n = triangle.size(); if (n == 0) return 0; int f[] = new int[triangle.size()]; f[0]= triangle.get(0).get(0); for (int i = 1; i < triangle.size(); i++) for (int j = triangle.get(i).size()-1; j >= 0; j--) { if (j == 0) f[j] = f[j] + triangle.get(i).get(j); else if (j == triangle.get(i).size() - 1) f[j] = f[j-1] + triangle.get(i).get(j); else f[j] = Math.min(f[j-1], f[j]) + triangle.get(i).get(j); } int ans = Integer.MAX_VALUE; for (int i = 0; i < f.length; i++) ans = Math.min(ans, f[i]); return ans; }}
0 0
- LeetCode : Triangle
- [LeetCode] Triangle
- [Leetcode] Triangle
- [LeetCode] Triangle
- 【leetcode】Triangle
- LeetCode - Triangle
- [LeetCode]Triangle
- [Leetcode]Triangle
- [leetcode]Triangle
- Leetcode: Triangle
- [LeetCode] Triangle
- LeetCode-Triangle
- [leetcode] Triangle
- [LeetCode] Triangle
- LeetCode - Triangle
- 【Leetcode】Triangle
- LeetCode | Triangle
- [leetcode]Triangle
- mac配置环境变量
- datagridview复制粘贴行与单元格内容
- Pascal's Triangle
- Android Studio 三种添加插件的方式
- Install Subversion 1.8.9 ( SVN Client ) on CentOS/RHEL
- LeetCode Triangle
- Android Log 跟踪
- 怎么清除Android开发中的垃圾代码?
- SDL图形库
- CentOS安装TortoiseSVN 客户端
- Linux命令之目录与路径
- mosquitto 使用时出现的一些问题及其解决办法
- SpringMVC集中传参方式
- mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication