One, dynamic programming algorithm
As we all know, the time complexity of the recursive algorithm is very high (2^n), and the dynamic programming algorithm can solve this kind of problem, the time complexity of the dynamic programming algorithm is (N^2). The dynamic programming algorithm is the solution of the space displacement time, it may be difficult to understand at first, and the drawing may understand a lot.
Two, dynamic programming algorithm analysis
Let me give you an example:
{7,0,0,0,0},{3,8,0,0,0},{8,1,0,0,0},{2,7,4,4,0},{4,5,2,6,5} This two-dimensional array, please, the top level to the bottom, The maximum value (that is, the longest path of the tree) that can only be added by both ends.
Analysis:
(1) The first step: there is the bottom to the upper level, because this is a pyramid shape, the bottom up, you can end up to a value, this value is the maximum value,
(2) Each layer is added and then the maximum number is compared. That
Third, the code implementation
@Test Public voidtest2 () {int[] arr={ {7,0,0,0,0}, {3,8,0,0,0}, {8,1,0,0,0}, {2,7,4,4,0}, {4,5,2,6,5} }; intmax = Maxsumnew (arr,5); SYSTEM.OUT.PRINTLN (max); } /*** Dynamic Planning *@paramarr *@paramN *@param * @return */ Public intMaxsumnew (intArr[][],intN) { if(arr==NULL){ return0; } int[] max =New int[N][n]; for(inti = n-1; I >=0; i--){ for(intj = 0; J <= I; J + +){ if(i==n-1) {Max[n-1][J] = arr[n-1][j]; }Else{Max[i][j]= Math.max (max[i+1][j],max[i+1][j+1]) +Arr[i][j]; } } } returnMax[0][0]; }
The above is the summary of the younger brother, if there is an incorrect place, also please correct me.
Reference url:http://blog.csdn.net/baidu_28312631/article/details/47418773
Dynamic programming Algorithm (Java)