Given an integer array nums
, return true
if there exists a triple of indices (i, j, k)
such that i < j < k
and nums[i] < nums[j] < nums[k]
. If no such indices exists, return false
.
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.
Constraints:
1 <= nums.length <= 5 * 105
-231 <= nums[i] <= 231 - 1
O(n)
time complexity and O(1)
space complexity?When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're trying to find three numbers that get bigger in a sequence. The brute force method involves checking every possible combination of three numbers to see if they fit the rule.
Here's how the algorithm would work step-by-step:
def increasing_triplet_subsequence_brute_force(numbers):
list_length = len(numbers)
for first_index in range(list_length - 2):
first_number = numbers[first_index]
for second_index in range(first_index + 1, list_length - 1):
second_number = numbers[second_index]
# Ensure the second number is greater than the first
if second_number > first_number:
for third_index in range(second_index + 1, list_length):
third_number = numbers[third_index]
# Final check for triplet completion
if third_number > second_number:
return True
# No increasing triplet found after all iterations
return False
The goal is to find if there are three numbers that appear in order, where each number is bigger than the one before it. Instead of checking all combinations, we keep track of the smallest two numbers we've seen so far and use those to quickly determine if we've found our increasing sequence.
Here's how the algorithm would work step-by-step:
def increasing_triplet_subsequence(numbers) -> bool:
smallest_number_seen = float('inf')
second_smallest_number_seen = float('inf')
for number in numbers:
if number <= smallest_number_seen:
# Found a new smallest number
smallest_number_seen = number
elif number <= second_smallest_number_seen:
# Found a new second smallest number
second_smallest_number_seen = number
else:
# We found a number bigger than
# both smallest and second smallest
return True
# If we didn't find a triplet
return False
Case | How to Handle |
---|---|
Empty or null input array | Return false immediately since a triplet cannot exist. |
Array with fewer than 3 elements | Return false immediately since a triplet cannot exist. |
Array with all identical values | The algorithm should correctly return false as no strictly increasing subsequence is possible. |
Array sorted in descending order | The algorithm should correctly return false as no strictly increasing subsequence is possible. |
Array with a long decreasing sequence followed by increasing elements | The algorithm should eventually find the increasing elements and return true if a triplet is formed. |
Array contains very large or very small integers (potential for overflow in naive approaches) | The chosen algorithm should avoid arithmetic operations that could lead to overflow, preferring comparisons. |
Array with duplicates interspersed amongst increasing values | The algorithm's logic should handle duplicates correctly, ensuring the subsequence is strictly increasing even with repeated values. |
Large array size (scalability considerations) | The algorithm should have a time complexity of O(n) to handle large arrays efficiently; approaches with higher complexity will be less suitable. |