334. Increasing Triplet subsequence

Source: Internet
Author: User

I did it once, but I can't remember.

1Now set an X, record the smallest of the three numbers, Y record the middle one, initialize X is nums[0],y is Integer.max_value .2 3 then start looking back from 1:4 51. If this number is larger than Y, because this time x <y, if you find a third that is larger than Y, then return true6 72If this number is not larger than Y, there are two things:8 91) is smaller than x (or equal to x), the x is updated to the current position, but Y does not move, X is the minimum number of potentially next set, Y still indicates that the second number, if the next group found greater than Y, can still return true. Ten  One2no smaller than x, that is, between the current X and Y, then the update y,x unchanged, indicating that a group of XY failed. Why this must be found, because the X is already updated first (because the X is updated in 2.1), less than the original is less than X, Y (because the condition 2 illustrates this), so this is a smaller set of XY, more likely to succeed.  A  -If the end of the entire array of traversal has not found the answer, then return false

That's probably it. Hope to remember!!

1      Public BooleanIncreasingtriplet (int[] nums) {2         if(Nums.length < 3) {3             return false;4         }5         intx = nums[0];6         inty =Integer.max_value;7          for(inti = 1; i < nums.length; i++) {8             if(Nums[i] >y) {9                 return true;Ten}Else { One                 if(Nums[i] <=x) { Ax =Nums[i]; -}Else { -y =Nums[i]; the                 } -             } -         } -         return false; +}

Time complexity is O (n)

334. Increasing Triplet subsequence

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.