13.1 Triangle
来源:互联网 发布:淘宝卖家代理怎么发货 编辑:程序博客网 时间:2024/06/08 15:51
Link:
My thought: 1 What does adjacent number mean?
e.g. if the triangle is as below. The adjacent numbers means those connected with lines. So the 0 indexed on the top level, is adjacent to the 0 and 1 index in the second level.
Approach I: from Dai's code. O(1) solution. Why time out?
It uses the triangle itself to save the intermediate sums.
triangle[i][j] += min(triangle[i+1][j], triangle[i+1][j+1])
public class Solution { public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) { int n = triangle.size(); for(int i = n -2; i>=0; i--){//i is the row index for(int j = 0; j < i+1; j++){//j: col index. Each row i has i numbers, so j belongs to[0, i] triangle.get(i).set(j, triangle.get(i).get(j) + Math.min(triangle.get(i+1).get(j), triangle.get(i+1).get(j+1))); } } return triangle.get(0).get(0); }}
http://blog.csdn.net/linhuanmars/article/details/23230657
这段代码的含义如下图所示:(see Triangle.png)
0 0
- 13.1 Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- Triangle
- C语言 回调函数的使用:(实例对比)
- sizeof运算符
- 【足迹C++primer】59、模板左值右值的转换
- lightoj 二分题简要题解
- C语言之动态内存分配
- 13.1 Triangle
- 【足迹C++primer】60、重载和模板
- 指针与结构体、函数指针
- 九大排序算法再总结
- 网站建设所需要考虑的架构问题
- js封装tab选项卡--半完成,纯代码框架(原创)
- C语言求200`300之间的所有素数
- 如果遇到实验室事故
- Web高性能开发总结