LeetCode——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.

原题链接:https://oj.leetcode.com/problems/triangle/

题目:给定一个三角形。找出从顶部究竟部的最短路径和。每一步你能够移动到下一行的邻接数字。

思路:最初的理解是:最短路径和能够看成是找出每一行中的最小值。最后求和。

	public int minimumTotal1(List<List<Integer>> triangle) {
		int sum = 0;
		for (int i = 0; i < triangle.size(); i++) {
			int min = triangle.get(i).get(0);
			for (int j = 0; j < triangle.get(i).size(); j++) {
				if (triangle.get(i).get(j) < min)
					min = triangle.get(i).get(j);
			}
			sum += min;
		}
		return sum;
	}

提交后发现错了。要是这样也太简单了。

要求的并非每行的最小值,而应该这样理解:到达每一个元素的最小路径和为到上一层与它相邻两个元素最短路径和中较小者加上该元素的值。

所以,解法能够例如以下:

	public int minimumTotal(List<List<Integer>> triangle) {
	    for(int i = triangle.size() - 2; i >= 0; i--)
	    {
	        for(int j = 0; j < triangle.get(i).size(); j++)
	        {
	            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);
	}

reference : http://www.douban.com/note/272435660/

时间: 2024-08-01 22:42:41

LeetCode——Triangle的相关文章

LeetCode -- Triangle 路径求最小和( 动态规划问题)

人们常说"细节决定成败". 编码工作中,同样需要关注细节. 本文将给出3个小实例来说明编码中关注细节的重要性,同时给出作者对如何注意编码细节的一点见解(说的不对,请指正). 例1 这个问题如此地显而易见,竟然没有被发现. List<int> numList = new List<int>(); numList.Add(3); numList.Add(1); numList.Add(4); numList.Add(2); numList.Add(5); numLi

[LeetCode] Triangle(&#39;Bottom-up&#39; DP)

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

Leetcode:Triangle 三角形塔最小路径和

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 botto

LeetCode: Triangle [120]

[题目] 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 

[leetcode]Triangle @ Python

原题地址:https://oj.leetcode.com/problems/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

LeetCode: Triangle 解题报告

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

[LeetCode] 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

leetcode 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

LeetCode Triangle 三角形(最短路)

题意:给一个用序列堆成的三角形,第n层的元素个数为n,从顶往下,每个元素可以选择与自己最近的两个下层元素往下走,类似一棵二叉树,求最短路. [2], [3,4], [6,5,7], [4,1,8,3] 注意:这里可以2->3>5>1,也可以2->4>5->1,隔层相邻就可以走. 思路:可以从下往上走,也可以从上往下走.都是O(n)的空间,平方阶的复杂度. 从下往上可能更简洁,因为比较到最后只有一个元素,就是为答案了,速度自然也就快,每遍历一层就有1个被淘汰. 然而我一开