Lee他Code Triangle

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;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-18 10:24:09

Lee他Code Triangle的相关文章

lab1-Junit&amp;Eclemma

Software Testing, Lab 1 March 10. 2016, by 赵国佺(3014218108) 一.     environment setup Firstly, I download the eclemma Zip from our ST's course web. Because it is my first time to add plugins into Eclipse. So I search it on the Internet, by the help of

关于html-三角的制作

因为最近看到别人写的不错的样式,所以就想自己实现,但是呢用到了一个三角形,所以稍微研究一下. 效果是这样的: 注意是下边那个浅色三角,感觉书签的效果有木有.看着很有层次感. 接下来就是实现了,利用border这个属性. 这里的话主要是 border-top.border-left.border-right.border-bottom 可以在css手册中查到(http://css.doyoe.com/) 先拉个空白写吧,我把几个属性都写上. code: .triangle{ width:0; he

使用Tslib在触摸屏上显示汉字【转】

转自:http://www.latelee.org/embedded-linux/use-tslib-to-display-chinese-character.html 终于到了在触摸屏上显示汉字了,真正写代码.测试的时间是1天,在此之前的一切准备.学习花费约2周到3周时间.而为了获取触摸屏上显示的图像,花费约2天.由于网络驱动已经接近于放弃状态,NFS用不了,只好用U盘来回复制(即使没有耐心也必须有耐心了).明明在内核中选择了支持NTFS格式的读写,但却不能将开发板上的文件复制到U盘,而用另一

Leet Code OJ 119. Pascal&#39;s Triangle II [Difficulty: Easy]

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 翻译: 给定一个下标k,返回第k行的杨辉三角. 例如给定k=3,返回[1,3,3,1]. 提示:你可以优化你的算法,让它只使用O(k)的额

Leet Code OJ 118. Pascal&#39;s Triangle [Difficulty: Easy]

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 翻译: 给定一个数numRows,产生前numRows行的杨辉三角(即贾宪三角形.帕斯卡三角形). 分析: 除了每行首尾是1以外,其他元素均可由上行推出,本方案采用lastLine保存上行数

【Codechef】A Triangle and Two Squares-Problem Code: SQRTRI

题意: 给出一个边长为a的正方形,其左下角坐标为(0,0),右上角坐标为(a,a). 此外,再给出一个边长为b的小正方形,以及它的左下角坐标(x,y),其右上角. 坐标为(x+b,y+b),并且保证小正方形一定在大正方形内(有可能会与大正方形的边重合,但不会超出大正方形). 问是否存在一个在顶点都在大正方形边上的三角形,满足三角形一条边在包含小正方形的一条边(小正方形的一条边在三角形的一条边上),并且三角形包含小正方形(小正方形在三角形内部,正方形的顶点或者边可以与三角形边重合). 若存在,输出

Triangle 1.6 (A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator)

Triangle A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator. Version 1.6 Copyright 1993, 1995, 1997, 1998, 2002, 2005 Jonathan Richard Shewchuk 2360 Woolsey #H / Berkeley, California 94705-1927 Bugs/comments to [email protected] Creat

Solution to Triangle by Codility

question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one hand, there is no false triangular. Given the array has been sorted, if A[i]+A[i+1]>A[i+2], we can prove the existence of the triangle. for array A i

120. Triangle

Problem Statement:  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 to