KatInterview

334. Increasing Triplet Subsequence

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to determine if an integer array nums contains an increasing subsequence of length 3. This means we need to find three indices i, j, k such that i < j < k and nums[i] < nums[j] < nums[k]. The follow-up question specifically requests a solution with O(n) time complexity and O(1) space complexity, which guides us towards an efficient, single-pass algorithm.

Solution

Increasing Triplet Subsequence in Python

class Solution:
    def increasingTriplet(self, nums: List[int]) -> bool:
        # 'first' stores the smallest element of a potential increasing subsequence.
        # 'second' stores the second smallest element.
        first = float('inf')
        second = float('inf')
        
        # Iterate through each number in the array.
        for n in nums:
            # If the current number is smaller than or equal to 'first',
            # it becomes the new candidate for the first element.
            if n <= first:
                first = n
            # If the current number is smaller than or equal to 'second'
            # (which implies it's greater than 'first'), it becomes the new
            # candidate for the second element. This makes the potential subsequence
            # tighter, increasing the chance to find a third element.
            elif n <= second:
                second = n
            # If the current number is greater than 'second' (which implies it's also > first),
            # we have found an increasing triplet. The actual triplet exists because
            # 'second' is only updated when a smaller 'first' has been seen before.
            else:
                return True
        
        # If the loop completes, no increasing triplet was found.
        return False

Increasing Triplet Subsequence in Java

class Solution {
    public boolean increasingTriplet(int[] nums) {
        // 'first' stores the smallest element of a potential increasing subsequence.
        // 'second' stores the second smallest element.
        int first = Integer.MAX_VALUE;
        int second = Integer.MAX_VALUE;
        
        // Iterate through each number in the array.
        for (int n : nums) {
            // If the current number is smaller than or equal to 'first',
            // it becomes the new candidate for the first element.
            if (n <= first) {
                first = n;
            }
            // If the current number is smaller than or equal to 'second'
            // but greater than 'first', it becomes the new candidate for the second element.
            // This makes the potential subsequence tighter, increasing the chance to find a third element.
            else if (n <= second) {
                second = n;
            }
            // If the current number is greater than 'second', we have found an increasing triplet.
            // The actual triplet exists because 'second' is only updated when a smaller
            // 'first' has been seen before. So a number smaller than 'second' exists, and
            // the current number 'n' is greater than 'second'.
            else {
                return true;
            }
        }
        
        // If the loop completes, no increasing triplet was found.
        return false;
    }
}

Increasing Triplet Subsequence in C++

class Solution {
public:
    bool increasingTriplet(vector<int>& nums) {
        // 'first' stores the smallest element of a potential increasing subsequence.
        // 'second' stores the second smallest element.
        int first = INT_MAX;
        int second = INT_MAX;
        
        // Iterate through each number in the array.
        for (int n : nums) {
            // If the current number is smaller than or equal to 'first',
            // it becomes the new candidate for the first element.
            if (n <= first) {
                first = n;
            } 
            // If the current number is smaller than or equal to 'second'
            // but greater than 'first', it becomes the new candidate for the second element.
            // This makes the potential subsequence tighter, increasing the chance to find a third element.
            else if (n <= second) {
                second = n;
            } 
            // If the current number is greater than 'second', we have found an increasing triplet.
            // The actual triplet exists because 'second' is only updated when a smaller
            // 'first' has been seen before. So a number smaller than 'second' exists, and
            // the current number 'n' is greater than 'second'.
            else {
                return true;
            }
        }
        
        // If the loop completes, no increasing triplet was found.
        return false;
    }
};

Increasing Triplet Subsequence in TypeScript

function increasingTriplet(nums: number[]): boolean {
    // 'first' stores the smallest element of a potential increasing subsequence.
    // 'second' stores the second smallest element.
    let first = Number.POSITIVE_INFINITY;
    let second = Number.POSITIVE_INFINITY;

    // Iterate through each number in the array.
    for (const n of nums) {
        // If the current number is smaller than or equal to 'first',
        // it becomes the new candidate for the first element.
        if (n <= first) {
            first = n;
        } 
        // If the current number is smaller than or equal to 'second'
        // but greater than 'first', it becomes the new candidate for the second element.
        // This makes the potential subsequence tighter, increasing the chance to find a third element.
        else if (n <= second) {
            second = n;
        } 
        // If the current number is greater than 'second', we have found an increasing triplet.
        // The actual triplet exists because 'second' is only updated when a smaller
        // 'first' has been seen before. So a number smaller than 'second' exists, and
        // the current number 'n' is greater than 'second'.
        else {
            return true;
        }
    }

    // If the loop completes, no increasing triplet was found.
    return false;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.