[Ctci] min. orderly adjustment

Source: Internet
Author: User

Minimum adjustment and order

Title Description

There is an array of integers, write a function to find the index m and N, as long as the elements between M and N are ordered, the entire array is orderly. Note: The n-m should be as small as possible, that is to say, find the shortest sequence that meets the criteria.

Given an int array of size n, return a two-tuple representing the starting and ending points of the ordered sequence. (The original sequence position is labeled starting from 0, Wakahara sequence, return [0,0]). Ensure that the elements in a are positive integers.

Test examples:
[1,4,6,5,9,10],6
return: [2,3]


Find the first pair of reversed pairs on the left and the first pair on the right, then find the maximum and minimum values between the two reverse pairs, and then extend the boundary to the minimum and maximum values respectively.

1 classRearrange {2  Public:3     intFindendofleft (vector<int> &A) {4          for(inti =0; I < a.size (); ++i) {5             if(A[i] < a[i-1])returnI1;6         }7         returnA.size ()-1;8     }9     intFindstartofright (vector<int> &A) {Ten          for(inti = a.size ()-2; I >=0; --i) { One             if(A[i] > a[i+1])returni +1; A         } -         return 0; -     } the     intShrinkleft (vector<int> &a,intMin_idx,intstart) { -         intcomp =A[min_idx]; -          for(inti = Start-1; I >=0; --i) { -             if(A[i] <= comp)returni +1; +         } -         return 0; +     } A     intShrinkright (vector<int> &a,intMax_idx,intstart) { at         intcomp =A[max_idx]; -          for(inti = start; I < a.size (); ++i) { -             if(A[i] >= comp)returnI1; -         } -         returnA.size ()-1; -     } invector<int> Findsegment (vector<int> A,intN) { -         //Write code here to         intEnd_left =Findendofleft (A); +         intStart_right =findstartofright (A); -         intMin_idx = End_left +1; the         if(Min_idx >= a.size ())return{0,0}; *         intMax_idx = Start_right-1; $          for(inti = End_left; I <= start_right; ++i) {Panax Notoginseng             if(A[i] < A[MIN_IDX]) Min_idx =i; -             if(A[i] > a[max_idx]) max_idx =i; the         } +         intLeft_idx =Shrinkleft (A, Min_idx, end_left); A         intRight_idx =shrinkright (A, Max_idx, start_right); the         return{left_idx, right_idx}; +     } -};

[Ctci] min. orderly adjustment

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.