334. Increasing Triplet Subsequence
Given an integer array
nums, returntrueif there exists a triple of indices(i, j, k)such thati < j < kandnums[i] < nums[j] < nums[k]. If no such indices exists, returnfalse.Example 1:
Input: nums = [1,2,3,4,5]
Output: true
Explanation: Any triplet where i < j < k is valid.Example 2:
Input: nums = [5,4,3,2,1]
Output: false
Explanation: No triplet exists.Example 3:
Input: nums = [2,1,5,0,4,6]
Output: true
Explanation: The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6.
def increasingTriplet(self, nums: List[int]) -> bool:
if len(nums) < 3:
return False
first = float('inf')
second = float('inf')
for num in nums:
if num <= first:
first = num # Update the smallest number
elif num <= second:
second = num # Update the second smallest number
else:
# If we find a number greater than both, return True
return True
return False