class Solution:
def increasingTriplet(self, nums):
first = float('inf')
second = float('inf')
for n in nums:
if n <= first:
first = n
elif n <= second:
second = n
else:
return True
return False
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int first = INT_MAX, second = INT_MAX;
for (int n : nums) {
if (n <= first) {
first = n;
} else if (n <= second) {
second = n;
} else {
return true;
}
}
return false;
}
};
class Solution {
public boolean increasingTriplet(int[] nums) {
int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE;
for (int n : nums) {
if (n <= first) {
first = n;
} else if (n <= second) {
second = n;
} else {
return true;
}
}
return false;
}
}
var increasingTriplet = function(nums) {
let first = Infinity, second = Infinity;
for (let n of nums) {
if (n <= first) {
first = n;
} else if (n <= second) {
second = n;
} else {
return true;
}
}
return false;
};
Given an integer array nums
, determine if there exists an increasing subsequence of length three (a triplet) such that nums[i] < nums[j] < nums[k]
for some indices i < j < k
.
Constraints:
True
if such a triplet exists, otherwise False
.nums = [1, 2, 3, 4, 5]
True
(since 1 < 2 < 3 or 2 < 3 < 4, etc.)
At first glance, the problem asks if there is any sequence of three numbers in nums
that are strictly increasing and appear in order. The most straightforward approach is to check all possible triplets, but this would be inefficient for large arrays.
To optimize, we can think about how to efficiently track potential candidates for the smallest and middle numbers as we traverse the array. If we can find two numbers that are smaller than a third encountered later, then we have our answer.
This leads to the idea of maintaining two variables: one for the smallest number seen so far (first
), and one for the next smallest (second
). If we ever find a number larger than both, we have found an increasing triplet.
The optimized solution uses a single pass through the array and two tracking variables.
first
and second
to very large values (infinity).
n
in nums
:
n
is less than or equal to first
, update first
(we found a new minimum).n
is less than or equal to second
, update second
(we found a new "middle" value).n
is greater than both first
and second
, so an increasing triplet exists. Return True
.False
.
first
and second
to the smallest possible values, we maximize the chance of finding a larger third number later.
Let's use the input nums = [2, 1, 5, 0, 4, 6]
.
first = inf
, second = inf
first
becomes 2first
becomes 1 (new minimum)second
; 5 <= inf
, so second
becomes 5first
becomes 0second
becomes 4True
.
Brute-force approach:
The key insight is that we can efficiently track potential candidates for the smallest and middle values as we iterate through the array. By maintaining two variables and updating them appropriately, we can detect an increasing triplet subsequence in a single pass and constant space. This approach is both elegant and efficient, making it suitable for large datasets and real-world applications.