Increasing Triplet Subsequence LeetCode
For the three increasing number, it’s quite easy, we just need to find a number which has one number smaller than it, and one number greater than it, so I used to arrays, one is used to keep the maximum number on the right, the other is used to keep the minum on the left, then loop the array again to find a number which is larger than left minum and smaller than right maxium
class Solution(object):
def increasingTriplet(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
l = len(nums)
if l < 3:
return False
less = [0] * l
great = [0] * l
minimum = nums[0]
maximum = nums[-1]
i = 0
while i < l:
minimum = min(nums[i],minimum)
less[i] = minimum
i += 1
i = l - 1
while i > 0:
maximum = max(nums[i],maximum)
great[i] = maximum
i -= 1
i = 0
while i < l:
if nums[i] > less[i] and nums[i] < great[i]:
return True
i += 1
return False
class Solution(object):
def increasingTriplet(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
l = len(nums)
if l < 3:
return False
less = [0] * l
great = [0] * l
minimum = nums[0]
maximum = nums[-1]
i = 0
while i < l:
minimum = min(nums[i],minimum)
less[i] = minimum
i += 1
i = l - 1
while i > 0:
maximum = max(nums[i],maximum)
great[i] = maximum
i -= 1
i = 0
while i < l:
if nums[i] > less[i] and nums[i] < great[i]:
return True
i += 1
return False
评论