Dynamic programming Algorithm (Java)

Source: Internet
Author: User

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)

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.