Leetcode DFS & DP triangle

Source: Internet
Author: User

Triangle total accepted: 17536 total submissions: 65508my submissions

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 is11(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.



Given a triangular array, each position in the triangular array has a numerical value.
Find a path from the top to the bottom to minimize the total number in the path.
You can go left or right of the next row at the current position each time.
Thought 1: DFS + Memorandum
In the J position of line I, there are two options to go down: the J position to line I + 1 or the J + 1 position
The subproblem at the J position or J + 1 position in line I + 1 is the same as the original problem and can be solved recursively by DFS.
Use an array _ Min [I] [J] To save the value of the solution process to avoid repeated computation.
Complexity: time O (N ^ 2), Space O (N ^ 2)


Idea 2: DP + scrolling Array
If DP [I] [J] is set to the minimum value of the path at the position J of row I, the state transition equation is
DP [I] [J] = min (DP [I-1] [k]) + A [I] [J] Where DP [I-1] [k] represents the previous state that can reach the DP [I] [J] State
Here, K can take J-1 or J, a [I] [J] to indicate the value of this position.
Because row I only needs to use the data of the row I-1, you do not have to save the optimal value before the I-1.
Finally, you only need to use the array DP [J ].
Complexity: time O (N ^ 2), Space O (N)

// Train of Thought 1 vector <int> _ min, _ triangle; int DFS (int I, Int J) {if (I = _ triangle. size ()-1) {return _ triangle [I] [J];} If (j <0 | j> = _ triangle [I]. size () return int_max; If (_ Min [I] [J]! = Int_max) return _ Min [I] [J]; // a special value indicates DFS (I, j) return _ Min [I] [J] = min (DFS (I + 1, J), DFS (I + 1, J + 1 )) + _ triangle [I] [J];} int minimumtotal (vector <int> & triangle) {If (triangle. empty () return 0; _ triangle = triangle; _ min = vector <int> (triangle. size (), vector <int> (Triangle [triangle. size ()-1]. size (), int_max); Return DFS (0, 0);} // Train of Thought 2int minimumtotal (vector <int> & triangle) {If (triangle. empty () return 0; vector <int> dp (triangle. back (). size (), 0); // initialize int I = 0; for_each (Triangle [0]. begin (), triangle [0]. end (), [& I, & DP] (Int & V) {DP [I ++] = V ;}); // iteratively update dpfor (I = 1; I <triangle. size (); ++ I) {int dp_j_1 = DP [0]; for (Int J = 0; j <triangle [I]. size (); ++ J) {int TEM = DP [J]; If (j = triangle [I]. size ()-1) DP [J] = dp_j_1; DP [J] = min (dp_j_1, DP [J]) + triangle [I] [J]; dp_j_1 = TEM ;}} int _ min = int_max; For (Int J = 0; j <DP. size (); ++ J) _ min = min (_ min, DP [J]); Return _ min ;}


Leetcode DFS & DP triangle

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.