Lintcode Triangle Digital Triangle

Source: Internet
Author: User
Tags lintcode

Chinese Description:
Given a number triangle, find the smallest path from the top to the bottom and the. Each step can be moved to an adjacent number on the following line.

Examples
For example, give the following number triangles:
[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]
Minimum path from top to bottom and 11 (2 + 3 + 5 + 1 = 11).

Attention
If you do this with only the extra space complexity O (n), you can get a bonus, where n is the total number of rows in the number triangle.

中文版 Version:
Given a triangle, find the minimum path sum from top to bottom. Each step you'll move to adjacent numbers on the row below.

Example
For example, given the following triangle
[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]
The minimum path sum is one bottom (i.e., 2 + 3 + 5 + 1 = 11).

Note
Bonus Point if your are able to does this using only O (n) extra spaces, where n is the total number of rows in the triangle.

public class Solution {/** * @param triangle:a List of lists of integers.
     * @return: An integer, Minimum path sum. ///////////from top to bottom minimum path and equal to minimum path and////To start from penultimate layer, the minimum path length of each number from the bottom to each layer equals the smaller value in the minimum path length of the lower adjacent digits from the bottom to the layer, plus the value of the number in that layer public I NT Minimumtotal (arraylist<arraylist<integer>> triangle) {list<integer> List = new ArrayList<
        Integer> ();
                for (int i = Triangle.size ()-2; I >= 0; i--) {for (int j = 0; J < Triangle.get (i). Size (); j + +) {
                int value = Math.min (Triangle.get (i+1). Get (j), Triangle.get (i+1). Get (j+1)) + triangle.get (i). get (j);
            Triangle.get (i). Set (J,value);
    } return Triangle.get (0). Get (0); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.