Algorithm about Subarrays & substrings

Source: Internet
Author: User

The Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which have the largest sum.

e.g. [−2,1,−3,4,−1,2,1,−5,4] -> [4,−1,2,1] = 6 .

1  Public voidMaxsubsimple (int[] Array) {2     if(Array = =NULL|| Array.Length = = 0) {3System.out.println ("Empty Array");4     }5     intmax = Array[0], cursum = array[0];6      for(inti = 1; i < Array.Length; i++) {7Cursum = cursum > 0? Cursum +Array[i]: array[i];8max = max > cursum?max:cursum;9     }TenSystem.out.println ("Max Sub-array sum is:" +max); One } A  Public voidMaxsubfull (int[] Array) { -     if(Array = =NULL|| Array.Length = = 0) { -System.out.println ("Empty Array"); the     } -     intmax = Array[0], cursum = array[0]; -     intStart = 0, end = 0; -     intfinal_s = 0, final_t = 0; +      for(inti = 1; i < Array.Length; i++) { -         if(Cursum < 0) { +Cursum =Array[i]; AStart =i; atEnd =i; -}Else { -Cursum = Cursum +Array[i]; -End =i; -         } -         if(Max <cursum) { inMax =cursum; -final_s =start; tofinal_t =end; +         } -     } theSystem.out.println ("Sub Array: (" +final_s+ "," +final_t+ ")"); *System.out.println ("Max Sub-array sum is:" +max); $}
View Code

No.2 Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which have the largest product.

e.g. [2,3,-2,4] -> [2,3] = 6 .

1  Public intMaxproduct (int[] A) {2         intPremin = A[0], Premax = a[0], max = a[0];3          for(inti = 1; i < a.length; i++) {4             intTMP1 = Premin *A[i];5             intTMP2 = Premax *A[i];6Premin =math.min (A[i], math.min (TMP1, TMP2));7Premax =Math.max (A[i], Math.max (TMP1, TMP2));8Max =Math.max (Premax, max);9         }Ten         returnMax; One}
View Code

No.3 LIS (longest increasing subarray)

The increasing subarray does ' t have to be contiuous nor same diff

e.g. [1, 6, 8, 3, 7, 9], [1, 3, 7, 9] = 4

DP, O (n^2)

1  Public voidFindlis (int[] Array) {2     if(Array = =NULL|| Array.Length = = 0) {3System.out.println ("Empty array");4         return;5     }6     intLen =Array.Length;7     int[] DP =New int[Len];8     intLis = 1;9      for(inti = 0; i < Len; i++) {TenDp[i] = 1; One          for(intj = 0; J < I; J + +) { A             if(Array[j] < Array[i] & Dp[j] + 1 >Dp[i]) { -Dp[i] = Dp[j] + 1; -             } the         } -         if(Dp[i] >lis) { -Lis =Dp[i]; -         } +     } -System.out.println ("Length of longest increasing subarray:" +lis); +}
View Code

DP + Binary Search, O (NLOGN)

Maintain an array maxval[i], record the minimum value of the largest element in the increment subsequence of length I, and for each element in the array, examine the largest element of which subsequence it is, and binary update maxval array-the beauty of programming

1  Public voidBinsearch (int[] Maxval,intMaxLen,intx) {2     intleft = 0, right = maxlen-1;3      while(Left <=Right ) {4         intMid = left + (right-left)/2;5         if(Maxval[mid] <=x) {6left++;7}Else {8right--;9         }Ten     } OneMaxval[left] =x; A } -  Public voidLIS (int[] Array) { -     if(Array = =NULL|| Array.Length = = 0) { theSystem.out.println ("Empty array"); -         return; -     } -     intLen =Array.Length; +     int[] Maxval =New int[Len]; -Maxval[0] = array[0]; +     intMaxLen = 1; A      for(inti = 0; i < Len; i++) { at         if(Array[i] > maxval[maxlen-1]) { -maxval[maxlen++] =Array[i]; -}Else { - Binsearch (Maxval, MaxLen, Array[i]); -         } -     } inSystem.out.println ("Length of longest increasing subarray:" +maxlen); -}
View Code

No.4 LCS (Longest Common Subarray)

Algorithm about Subarrays & substrings

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.