I<j<k

Source: Internet
Author: User

Question:


Given an array, does it exist that

I < J < K where a[i] < A[j] < [K].

 option 1.// brute force.// given any index i, return true  if there exists:// A[m] < A[i], m < i// A[n]  > a[i], n > iboolean haslessinleft (int[] a, int i) {   for  (int index = 0 ; index < i ; index ++)    {    if  (A[index] < a[i])        Return true;  }  return false;} Boolean hasmoreinright (int[] a, int i) { ... }// o (n ^ 2) Boolean  has3increasing (Int[] a) {  for  (int i = 0 ; i <  a.length ; i ++)   {    if  (Haslessinleft (a, i)  && hasmoreinright (a, i))      &nbsP;return true;  }  return false;}  a better solution?// maintain min, midboolean has3increasing (int[]  A) {  integer min = null;  integer mid = null;     for  (Int i : a)   {    if  (min ==  null | |  i < min)     {      min = i;     }    else if  (mid == null | |  i < mid)     {      mid = i         }    else if  (i >  MID)  // This means there must be a min before mid.     {      return true;&nbSp;   }      }    return false;} 


I<j<k

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.