
Problem Statement
The task is to determine if there exists a triplet (i, j, k)
in a given array nums
such that:
- The indices satisfy the order
i < j < k
. - The elements at these indices satisfy the inequality
nums[i] < nums[j] < nums[k]
.
In other words, you are asked to find out whether there is a sequence of three strictly increasing numbers appearing in the array according to their order of indices. If such a triplet exists, the function should return true
, otherwise, it should return false
.
Examples
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
Approach and Intuition
To tackle this problem effectively, it is beneficial to think about how we could efficiently check for the conditions of having three increasing ordered numbers:
Initial Thoughts:
- A brute-force solution could involve checking all possible combinations of triplets in the array. However, this would be highly inefficient, especially with the upper constraint of the array’s length.
Optimization Insight:
- We should look out for the smallest number seen so far (
small
) and check if we can find a middle number (middle
) greater thansmall
but less than some numbers appearing aftermiddle
.
- We should look out for the smallest number seen so far (
Operational Steps:
- Traverse through the array while keeping track of the smallest value found so far — categorize it as
small
. - Look for another number that is larger than
small
yet less than another subsequent number, treating this asmiddle
. - If we can find another number greater than
middle
as we continue, then a triplet exists.
- Traverse through the array while keeping track of the smallest value found so far — categorize it as
Runtime Consideration:
- By tracking the needed values during a single pass in the array, or utilizing optimized data structures like stacks, we can considerably lower the runtime from the naive approach.
Edge Cases:
- Arrays with less than three numbers automatically don’t satisfy the conditions (though this is restricted by the constraints).
- Arrays where all elements are decreasing or all are same also fail.
From the example cases provided:
- Example 1 demonstrates a clear strictly increasing series.
- Example 2 illustrates a decreasing sequence, showing why it’s impossible to find a valid triplet.
- Example 3 highlights that the triplet need not be contiguous, merely increasing with correct index ordering.
Solutions
- Java
- Python
class Solution {
public boolean hasTripletSequence(int[] sequence) {
int low = Integer.MAX_VALUE;
int mid = Integer.MAX_VALUE;
for (int val: sequence) {
if (val <= low) {
low = val;
} else if (val <= mid) {
mid = val;
} else {
return true;
}
}
return false;
}
}
The provided Java code solves the problem of determining if an array contains an increasing triplet subsequence. A subsequence of three integers is considered increasing if each element is larger than the previous one.
Follow these main components of the solution strategy:
- Initialize two variables
low
andmid
with the highest possible integer values from the classInteger.MAX_VALUE
. - Traverse through each integer in the input array
sequence
. - Use an if-else structure to adjust the values of
low
andmid
based on the current integerval
:- If
val
is less than or equal tolow
, updatelow
to the value ofval
. - If
val
is greater thanlow
but less than or equal tomid
, updatemid
to the value ofval
. - If
val
exceeds bothlow
andmid
, it confirms the existence of an increasing triplet, and the function returnstrue
.
- If
- If the loop completes without finding such a triplet, return
false
.
This method effectively checks for the existence of an increasing triplet subsequence without needing to maintain or check all possible triplets, thereby optimizing performance in terms of time complexity.
class Solution:
def hasTripletSubsequence(self, sequence: List[int]) -> bool:
low = float("inf")
mid = float("inf")
for value in sequence:
if value <= low:
low = value
elif value <= mid:
mid = value
else:
return True
return False
This Python solution checks if there exists an increasing triplet subsequence in a given list of integers. The approach taken is efficient, using two variables, low
and mid
, initialized to infinity. As the code iterates through the sequence
, it updates low
and mid
based on the conditions:
- Set
low
to the currentvalue
if thevalue
is less than or equal tolow
. - If the
value
is greater thanlow
but less than or equal tomid
, then setmid
to thisvalue
. - If a
value
is found that is greater than bothlow
andmid
, it confirms the presence of an increasing triplet and the function returnsTrue
.
If the loop completes without finding such a triplet, the function returns False
. This method ensures an O(n) time complexity with O(1) space complexity, making it optimal for large sequences.
No comments yet.