Dynamic programming Exercises--chapter nine algorithm fifth to sixth exercises

Source: Internet
Author: User

1.Triangle

Given a triangle, find the minimum path sum from top to bottom. Each step of the 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 11 is (i.e., 2 + 3 + 5 + 1 = 11).

//if the final optimal path passes that point, the path from the starting point to that point must be the optimal one, but the optimal path from the starting point to the point is not necessarily the local optimal path, which may be larger than the neighboring point of the same stage . Public classSolution { Public intMinimumtotal (list<list<integer>>triangle) {        intn =triangle.size (); int[] f =New intN [n];//because each step can only walk near the number, so the longest horizontal is the same as the number of rows//Initialize         for(inti=0;i<n;i++){             for(intj=0;j<n;j++) {F[i][j]=Integer.max_value; }} f[0][0]=triangle.get (0). Get (0); //let F[i][j] represent the smallest path from the starting point to the i,j point//Minimum path and f[0][n-1]~f[n-1][n-1] minimum value         for(intI =1;i<n;i++){             for(intj=0;j<=i;j++){                if(j==0) F[i][j]=f[i-1][j]+Triangle.get (i). get (j); ElseF[i][j]=math.min (F[i-1][j],f[i-1][j-1]) +Triangle.get (i). get (j); }        }        intmin=Integer.max_value;  for(intI =0;i<n;i++){            if(f[n-1][i]<min) min=f[n-1][i]; }        returnmin; }}

Lite version: Because F[I][J] is related only to f[i-1][j] and f[i-1][j-1], which is only related to the previous line state and therefore can only request 2 lines of space, I's processing on%2 is good. If the first 2 lines are related to% 3, .... And so on

1  Public classSolution {2      Public intMinimumtotal (list<list<integer>>triangle) {3         intn =triangle.size ();4         int[] f =New int[2] [n];//because each step can only walk near the number, so the longest horizontal is the same as the number of rows5          6         //Initialize7          for(inti=0;i<n;i++){8              for(intj=0;j<n;j++){9F[i%2][j]=Integer.max_value;Ten             } One         } AF[0][0]=triangle.get (0). Get (0); -         //let F[i][j] represent the smallest path from the starting point to the i,j point -         //Minimum path and f[0][n-1]~f[n-1][n-1] minimum value the          for(intI =1;i<n;i++){ -              for(intj=0;j<=i;j++){ -                 if(j==0) -F[i%2][J]=f[(i-1)%2][j]+Triangle.get (i). get (j); +                 Else -F[i%2][J]=math.min (f[(i-1)%2][J],f[(i-1)%2][J-1]) +Triangle.get (i). get (j); +             } A         } at         intmin=Integer.max_value; -          for(intI =0;i<n;i++){ -             if(f[(n-1)%2][i]<min) -Min=f[(n-1)%2 ][i]; -         } -         returnmin; in     } -}

If the final optimal path passes that point, the path from the starting point to that point must be the optimal one, but the optimal path from the starting point to the point is not necessarily the local optimal path, which may be larger than the neighboring point of the same stage.
public class Solution {
public int minimumtotal (list<list<integer>> triangle) {
int n = triangle.size ();
Int[][] f = new Int[2][n]; Because each step can only walk near the number, so the longest horizontal is the same as the number of rows

Initialization
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
F[i%2][j]=integer.max_value;
}
}
F[0][0]=triangle.get (0). Get (0);
Let F[i][j] represent the smallest path from the starting point to the i,j point
Minimum path and f[0][n-1]~f[n-1][n-1] minimum value
for (int i =1;i<n;i++) {
for (int j=0;j<=i;j++) {
if (j==0)
f[i%2][j]=f[(i-1)%2][j]+triangle.get (i). get (j);
Else
F[i%2][j]=math.min (f[(i-1)%2][j],f[(i-1)%2][j-1]) +triangle.get (i). get (j);
}
}
int min=integer.max_value;
for (int i =0;i<n;i++) {
if (f[(n-1)%2][i]<min)
min=f[(n-1)%2][i];
}
return min;
}
}

Dynamic programming Exercises--chapter nine algorithm fifth to sixth exercises

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.