Give an array a, find a pair (I, J) make A[i] <= A[j] (i < j) and J-i Max

Source: Internet
Author: User

Title: Give an array A, find a pair (I, J) make A[i] <= a[j] (i <= j) and J-i Max, if there are multiple such position pairs, return I the smallest pair.

The most straightforward idea is that for each i from the end of the array start to find the first position of greater than or equal to A[i] j, Time complexity O (n^2).

1. pair<int,int> Find (Constvector<int> &A)2. {  3.intn =a.size (); 4.if(n = =0)  5.Throw NewInvalid_argument ("Array ' s size can ' t be 0!"); 6. 7.intTarget_i =0, Target_j =0; 8.intMax_len =0; 9. for(inti =0; I < n; ++i)Ten. {   One.intJ;  A. for(j = N1; J >= I; --j) -.if(A[j] >=A[i]) -. Break;  the.if(j-i+1>Max_len) -. {   -. Target_i =i;  -. Target_j =J;  +. Max_len = j-i+1;  -. }   +. }   A.  at.returnmake_pair<int,int>(Target_i, Target_j);  -. }

We have optimized the above algorithm slightly. When i=0, we assume that the most right position to find is greater than a[i] is j0, then for I=1, we do not need to consider the position of less than j0, because their interval length is less than j0+1, it is impossible to become the optimal solution.

1. pair<int,int> Find (Constvector<int> &A)2. {  3.intn =a.size (); 4.if(n = =0)  5.Throw NewInvalid_argument ("Array ' s size can ' t be 0!"); 6. 7.intTarget_i =0, Target_j =0; 8.intMax_len =0; 9. for(inti =0; I < n; ++i)Ten. {   One.intJ;  A.For  (j = n-1; j > target_j;--j)//   here just check target_j   -.if(A[j] >=A[i]) -. Break;  the.if(j-i+1>Max_len) -. {   -. Target_i =i;  -. Target_j =J;  +. Max_len = j-i+1;  -. }   +. }   A.  at.returnmake_pair<int,int>(Target_i, Target_j);  -. }

But the time complexity is still O (n^2). We can continue with the above thinking optimization. In fact, for position I to find the last greater than or equal to its position, do not need to look forward from the array every time, we can improve this place to change the time complexity O (n).

The process is this, for I, we first find I and its right side of the largest element of the position J, check whether the best solution is better than the current record, update. Then consider whether the maximum element position of the j+1 and its right side is greater than or equal to a[i], if so, J equals that position, repeat as above procedure, if no, then start from position i+1 again, but J still consider from current position, reason above already explained. So the complexity of Time becomes O (n).

Please refer to the code for details

Give an array a, find a pair (I, J) make A[i] <= A[j] (i < j) and J-i Max

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.