Increasing Triplet subsequence

Source: Internet
Author: User

Given an unsorted array return whether a increasing subsequence of length 3 exists or not in the array.

Formally the function should:

Return True if there exists I, J, K
such that Arr[i] < arr[j] < arr[k] given 0≤ i < J < kn-1 else return false.

Your algorithm should run in O (n) time complexity and O (1) space complexity.

Examples:
Given [1, 2, 3, 4, 5] ,
Return true .

Given [5, 4, 3, 2, 1] ,
Return false .

Main topic:

To an array of integers that do not have a sort, returns true if the array has three number increments, or false otherwise. where three-digit position relationship satisfies: Array[i] < ARRAY[J] < Array[k], I < J < K

In other words, the three numbers and the positions in the array are incremented as you go.

Idea One:

Iterate through all the elements in the array, three arrays and all cases, enumerate all possible judgments

Simple rough, high time complexity O (N ^ 3), Space complexity O (1)

1  Public classSolution {2      Public BooleanIncreasingtriplet (int[] nums) {3         if(Nums = =NULL|| Nums.length < 3)4             return false;5         BooleanFound =false;6          for(inti = 0; I < nums.length-2 &&!found; i++){7              for(intj = i + 1; J < nums.length-1 &&!found; J + +){8                  for(intK = j + 1; K < Nums.length; k++){9                     if(Nums[i] < Nums[j] && Nums[j] <Nums[k]) {TenFound =true; One                          Break; A}//if -}//For K -}//For J the}//For i -          -         returnfound; -}//Increasingtriplet +}

Java write, run time 166ms

Idea two:

Brush a number of such questions, the topic also has a hint, there is an O (N) solution. According to experience, individuals feel that there is no need to cite all the possibilities, we have to go straight to the results to see what the results need. There is no need to find out unnecessarily and add complexity. This question I actually have the thought, but did not think, looked the discussion area one writes in C + +. I was in the same direction as he thought, but I didn't think about it.

Wrote the Java version according to his idea.

1  Public classSolution {2      Public BooleanIncreasingtriplet (int[] nums) {3         if(Nums = =NULL|| Nums.length < 3)4             return false;5         intMin =Integer.max_value;6         intMID =Integer.max_value;7          for(intnum:nums) {8             if(Num <min)9Min =num;Ten             Else if(Num >min) { One                 if(Num >mid) A                     return true; -MID =num; -}//Else the}// for -          -         return false; -}//Increasingtriplet +}

Run Time 1ms

Discussion Area C + + solution Link: https://leetcode.com/discuss/95137/my-c-o-n-solution

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.