3. Division and Treatment study-the longest continuous increment subset in the search array

Source: Internet
Author: User

Research on the algorithm of divide and conquer the longest continuous increment subset in the search array
var cc=console
function Find_max_crossing_lenarray (A,low,mid,high) {
var max_left=mid,max_right=mid
var left_sum=1
var sum=0
for (Var i=mid;i>low;i--) {
SUM=A[I]-A[I-1]
if (sum==1) {
left_sum++
Max_left=i-1
}else{
Break
}
}
var right_sum=1
var sum=0
for (Var i=mid;iSum=a[i+1]-a[i]
if (sum==1) {
right_sum++
Max_right=i+1
}else{
Break
}
}
return [Max_left,max_right,left_sum+right_sum-1]
}
Search for the longest consecutive incremental subset across the midpoint
var arr=[13,-3,-2,-1,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7]
var Re=find_max_crossing_lenarray (arr,0,8,arr.length-1)
Cc.log (RE)//=>18,20,-7,12

Function Find_maximum_lenarray (a,low,high) {
    if (high==low) {
         return [low,high,0]
   }else{
        var mid= (Low+high) >>1
       /*[left_low,left_high,left_sum]*/
        var left=find_maximum_lenarray (a,low,mid)
        /*[right_low,right_high,right_sum]*/
        var right= Find_maximum_lenarray (A,mid+1,high)
       /*[cross_low,cross_high,cross _sum]*/
        var cross=find_max_crossing_lenarray (A,low,mid,high)

if (Left[2]>=right[2]&&left[2]>=cross[2]) {
return left
}else if (right[2]>=left[2]&&right[2]>=cross[2]) {
return right
}else if (cross[2]>=left[2]&&cross[2]>=right[2]) {
Return Cross
}
}
}
Searches for the longest contiguous increment subset in an array
var Re2=find_maximum_lenarray (arr,0,arr.length-1)
Cc.log (Re2)//=>-3,-2,-1 [1,3,3]

3. Division and Treatment study-the longest continuous increment subset in the search 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.