leetcode:132 Pattern

Source: Internet
Author: User

 Given a sequence of n integers a1, a2, ..., an, a-pattern is a subsequence ai, AJ, AK such that I < J < K A nd AI < ak < AJ.  Design an algorithm, takes a list of n numbers as input and checks whether there is a 132 The pattern in the list. Note:n'll is less than  15,000 1:input: [ 1, 2, 3, 4]output: Falseexplanation:there is no  132 pattern in the sequence. Example  2:input: [ 3, 1, 4, 2]output: Trueexplanation:there is a  pattern in the sequence: [1, 4, 2 3:input: [-1, 3, 2, 0]output: Trueexplanation:there is three  patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0]. 

I think this problem is hard, the difficulty first is to think of stack, the second is to maintain a min-max sequence such as: So at any time in the stack, non-overlapping is Pairs formed I n Descending order by their min value, which means the min value of Peek element in the stack was always the min value Glob Ally.

The idea is, we can use a stack to keep track of previous min-max intervals.

For each number in the num array

If Stack is empty:

    • Push a new Pair of into num stack

If stack is not empty:

    • If num < stack.peek().min , push a new Pair of into num stack

    • If num >= stack.peek().min , we first pop () out the peek element, denoted aslast

        • if  num  <  Last.max , we is done, return  true ;

        • if  num  >=  Last.max , we merge  num  into   last , which means  Last.max  =  num .
          Once we update  last , if stack was empty, we just push back  last .
          However, the crucial part was:
          If Stack is isn't empty, the updated  last   Might:

          • entirely covered stack.peek (), i.e.  last.min  <  Stack.peek ( ). Min   (which is always true) &&  Last.max  >=  Stack.peek (). Max , in which case we keep popping out Stack.peek ().
          • Form A 1-3-2 pattern, we is done, return  true

Refer TO:HTTPS://DISCUSS.LEETCODE.COM/TOPIC/68193/JAVA-O-N-SOLUTION-USING-STACK-IN-DETAIL-EXPLANATION/2

1 classpair{2         intmin, Max;3          PublicPair (intMinintmax) {4              This. Min =min;5              This. Max =Max;6         }7     }8      Public BooleanFind132pattern (int[] nums) {9stack<pair> stack =NewStack ();Ten          for(intn:nums) { One             if(Stack.isempty () | | n <stack.peek (). Min) Stack.push (NewPair (n,n)); A             Else if(N >Stack.peek (). Min) {  -Pair last =Stack.pop (); -                 if(N < Last.max)return true; the                 Else { -Last.max =N; -                      while(!stack.isempty () && n >=Stack.peek (). Max) Stack.pop (); -                     //at this time, n < Stack.peek (). Max (if stack is not empty) +                     if(!stack.isempty () && Stack.peek (). Min < N)return true; - Stack.push (last); +                 } A                  at             } -         } -         return false; -}

My method, line 15th, is different.

1  Public classSolution {2      Public classPair {3         intmin;4         intMax;5          PublicPair (intN1,intn2) {6Min =N1;7Max =N2;8         }9     }Ten      One      Public BooleanFind132pattern (int[] nums) { A         if(Nums.length < 3)return false; -Stack<pair> st =NewStack<pair>(); -          for(intn:nums) { the             if(St.isempty () | | n<=st.peek (). Min) St.push (NewPair (n, N)); -             Else { -                 if(N < St.peek (). Max)return true; -Pair last =St.pop (); +Last.max =Math.max (Last.max, n); -                  while(!st.isempty () && last.max>=St.peek (). Max) St.pop (); +                 if(!st.isempty () && Last.max>st.peek (). Min)return true; A St.push (last); at             } -         } -         return false; -     } -}

leetcode:132 Pattern

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.