334 ternary incremental sequence

Title: Given an unsorted array, the array is determined whether or not there is increasing subsequence length of 3.
Mathematical expression as follows:
    If there is a i, j, k, and satisfies 0 ≤ i <j <k ≤ n-1,
    so that arr [i] <arr [j ] <arr [k], returns true; otherwise false.
Description: The time requirements of the algorithm complexity is O (n), the spatial complexity is O (1).
Link: https: //leetcode-cn.com/problems/increasing-triplet-subsequence

Act One: Official Solution

Ideas: very clever, but since the subject for Triple sequences are asked to return in the presence of increasing, thus maintaining a an array, the last value is a maximum value, if the number is greater than the new maximum value, and then applied to the back, the first number is a minimum value, if the new number is between the first number and the second number, the second number is updated, if a length of up to 3, the result is returned.

# Execute when used: 56 ms, beat the 95.60% of users in all Python3 submission 
# memory consumption: 13.9 MB, defeated 51.50% of users in all Python3 submission 

# whole idea is to get the number of sequences to be updated in the best may be small 
from Typing Import List
 class Solution:
     DEF increasingTriplet (Self, the nums: List [int]) -> BOOL:
         IF len (the nums) == 0:
             return False 
        A = [the nums [0]]
         for I in the nums [. 1 :]:
             # last value is a maximum value, if greater than a maximum applied directly behind 
            IF I> a [-1 ]: 
                a = a +[I]
             # if it exceeds a minimum value, then a need to update, 
            # at this time may only be of a length of 1 or 2, whether the length is 12, the last value is updated or 
            elif I> a [0]: 
                a [ -1] = I
             # otherwise described first value or less, will replace the first value of 
            the else : 
                a [0] = I
             IF len (a) ==. 3 :
                 return True
         return False
 IF  the __name__ == ' __main__ ' : 
    duixiang = Solution () 
    A = duixiang.increasingTriplet ([])
     Print(a)
View Code

 

Guess you like

Origin www.cnblogs.com/xxswkl/p/12307101.html