*maximum Length palindromic sub-sequence of an Array.

Source: Internet
Author: User

/**/classpublicstaticint Maxlengthpalindrome (int//}

Solution One:

Implemtation is recursive, and it ' s much worse than O (n^2). Should use recursive with memorization and then can improve to O (n^2).
However, recursive memorization doesn ' t improve over bottom-up and it had costly overhead. Bottom-up is better in this problem.

 Public classSolution { Public Static voidMain (string[] args) {intArr[] =New int[] {4,1,2,3,4,5,6,5,4,3,4,4,4,4,4,4,4}; System.out.println (arr, maxlengthpalindrome0, Arr.length-1)); }     Public Static intMaxlengthpalindrome (int[] values,intIintj) {//Check if indexes cross all other//return 1 if index overlap for else condition below//return 0 if index i<j for condition if below        if(j<=i)returnJ-i+1; if(Values[i]==values[j])return2 + maxlengthpalindrome (values, i+1, j-1); Else returnMath.max (Maxlengthpalindrome (values, i+1, J), Maxlengthpalindrome (values, I, j-1)); }}

Solution Two:

The code with the memoization technique which produces O (n^2) complexity are

 Public intDplps (int[] A,intIintJ, integer[][] LPs) {        if(I >j)return0; if(Lps[i][j] = =NULL) {            if(i = =j) Lps[i][j]= 1; Else {                if(A[i] = =A[j]) lps[i][j]= 2 + dplps (A, i + 1, j-1, LPs); ElseLps[i][j]= Math.max (Dplps (A, i + 1, J, LPs), Dplps (A, I, J-1, LPs)); }        }        returnLps[i][j]; }

The function as,

Dplps (A, 0, a.length-1,new integer[a.length][a.length])

*maximum Length palindromic sub-sequence of an Array.

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.