Description
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
.
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.
Solution in Python
To solve the problem of finding if there exists an increasing triplet subsequence in the array nums
, we can leverage a greedy approach. The goal is to find three indices i < j < k
such that nums[i] < nums[j] < nums[k]
.
Key Idea:
We don’t need to track the exact indices of the triplet; we just need to determine if such a subsequence exists. The idea is to maintain two variables first
and second
which will represent the smallest and second smallest elements encountered so far, respectively. As we traverse through the array:
- We update
first
to the smallest value encountered. - We update
second
when we find a number larger thanfirst
but smaller thansecond
. - If we find a number larger than both
first
andsecond
, we know a triplet exists, and we can returnTrue
.
Algorithm:
- Initialize two variables
first
andsecond
with large values to represent the smallest and the second smallest numbers encountered so far. - Traverse the array
nums
.- If the current element is smaller than or equal to
first
, updatefirst
to this value (since we are looking for the smallest possible number). - If the current element is larger than
first
but smaller than or equal tosecond
, updatesecond
. - If the current element is larger than both
first
andsecond
, returnTrue
since we have found an increasing triplet.
- If the current element is smaller than or equal to
- If we complete the loop without finding such a triplet, return
False
.
Python
class Solution:
def increasingTriplet(self, nums: List[int]) -> bool:
# Initialize two variables to track the smallest and second smallest numbers
first = float('inf')
second = float('inf')
# Traverse through the array
for num in nums:
if num <= first:
# If current number is smaller than or equal to 'first', update 'first'
first = num
elif num <= second:
# If current number is greater than 'first' but smaller than or equal to 'second', update 'second'
second = num
else:
# If we find a number larger than both 'first' and 'second', return True
return True
# If no increasing triplet is found, return False
return False
Explanation of the Code:
first
andsecond
initialization: We start withfirst
andsecond
set to infinity (float('inf')
), which ensures that any number in the array will be smaller initially.- Loop through
nums
: For each numbernum
in the array:- If
num
is less than or equal tofirst
, it means we have found a new potential smallest number, so we updatefirst
. - If
num
is larger thanfirst
but less than or equal tosecond
, we updatesecond
, as this could be a potential second smallest number. - If
num
is greater than bothfirst
andsecond
, we returnTrue
because we have found a triplet that satisfies the condition.
- If
- Final return: If the loop finishes without returning
True
, that means no increasing triplet was found, so we returnFalse
.
Solution in C++
C++
class Solution {
public:
bool increasingTriplet(std::vector<int>& nums) {
// Edge case: If the array has less than 3 elements, we cannot have a triplet
if (nums.size() < 3) {
return false;
}
// Initialize two variables with large values
int first = INT_MAX, second = INT_MAX;
// Traverse through the array
for (int num : nums) {
// If the current number is smaller than or equal to the first, update the first
if (num <= first) {
first = num;
}
// If the current number is greater than first but smaller than or equal to the second, update the second
else if (num <= second) {
second = num;
}
// If the current number is greater than both first and second, we found a triplet
else {
return true;
}
}
// No increasing triplet was found
return false;
}
};
Solution in Javascript
JavaScript
var increasingTriplet = function(nums) {
// Initialize two variables, first and second, to hold the smallest and second smallest values we've encountered.
let first = Infinity;
let second = Infinity;
// Loop through the array of numbers
for (let num of nums) {
// Check if the current number is smaller than or equal to the 'first' variable
// If so, update 'first' to the current number. This means we're still looking for the smallest number.
if (num <= first) {
first = num;
}
// Otherwise, check if the current number is smaller than or equal to 'second'
// and greater than 'first'. If so, update 'second' to the current number. This means
// we're looking for the second number in the increasing triplet.
else if (num <= second) {
second = num;
}
// If the current number is greater than both 'first' and 'second', then we have found the third number
// in the increasing triplet and can return true.
else {
return true;
}
}
// If we complete the loop without finding a valid increasing triplet, return false.
return false;
};
Solution in Java
Java
class Solution {
public boolean increasingTriplet(int[] nums) {
// Step 1: Check for array length. If it's less than 3, it's impossible to find a triplet
if (nums.length < 3) {
return false;
}
// Step 2: Initialize two variables to track the first and second smallest elements
int first = Integer.MAX_VALUE; // This will track the smallest number
int second = Integer.MAX_VALUE; // This will track the second smallest number
// Step 3: Traverse through the array and attempt to find an increasing triplet
for (int num : nums) {
// If the current number is smaller than or equal to 'first', update 'first'
if (num <= first) {
first = num;
}
// If the current number is smaller than or equal to 'second' but larger than 'first', update 'second'
else if (num <= second) {
second = num;
}
// If we find a number greater than both 'first' and 'second', we have found a valid triplet
else {
return true;
}
}
// Step 4: If we complete the loop without finding such a triplet, return false
return false;
}
}