Skip to content

334. Increasing Triplet Subsequence (I/P)

Solve in Leetcode


Static Badge


Description

Solution

function increasingTriplet(nums: number[]): boolean {
    if (nums.length < 3) return false;

    let i = Number.MIN_SAFE_INTEGER;
    let j = Number.MAX_SAFE_INTEGER;

    for (let n of nums) {
        if (n <= i) {
            i = n; // i > n
        } else if (n <= j) {
            j = n; // j > i
        } else {
            return true; // k > j > i
        }
    }

    return false;
};