Triangle
来源:互联网 发布:linux mint18不能安装 编辑:程序博客网 时间:2024/06/08 10:37
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.
public class Solution { public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) { int n = triangle.size(); int [] res = new int [n]; ArrayList<Integer> row = triangle.get(n - 1); for (int i = 0; i < n; i++) {res[i] = row.get(i);} for (int i = n -2; i>= 0; --i){ row = triangle.get(i); for (int j = 0; j <= i ; j++) {res[j] = min(res[j], res[j + 1]) + row.get(j);} } return res[0]; }private int min(int a, int b) {return a < b? a : b;}}
0 0
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- swing 子窗口关闭时刷新父窗口
- 黑马程序员 网络编程
- python三目运算符
- 今天看了一个关于售前的小故事很值得深思,转给大家看看:)
- 学习tornado:异步
- Triangle
- .SPRING:您准备好了吗?
- JAVA NIO2模式之Proactor( JDK7 AIO异步网络IO初探)
- 手动Root Samsung Note3(android4.3版本)
- jfreechart
- 市委组织部考核项目——动态添加,删除行
- 解决Extjs4.1+Spring-Security3.1.0实现web登录发生的问题
- SBT模板-郁闷的出纳员(By Kuangbin)
- OpenGL ES 在 Android 开发库中 - 能不能再牛点儿