The problem. Given an integer array nums, return true if there are three indexes i < j < k with nums[i] < nums[j] < nums[k] — three values that increase from left to right — and false otherwise. They don't need to be next to each other. The follow-up asks for O(n) time and O(1) space.
Input: nums = [1, 2, 3, 4, 5]
Output: true e.g. 1 < 2 < 3
Input: nums = [5, 4, 3, 2, 1]
Output: false the values only fall
Input: nums = [2, 1, 5, 0, 4, 6]
Output: true e.g. 1 < 4 < 6, or 0 < 4 < 6Three nested loops check every i < j < k directly.
function increasingTriplet(nums) { const n = nums.length; for (let i = 0; i < n; i++) { for (let j = i + 1; j < n; j++) { for (let k = j + 1; k < n; k++) { if (nums[i] < nums[j] && nums[j] < nums[k]) return true; } } } return false;}Time O(n³), space O(1). With up to 500,000 numbers, hopeless.
Fix the middle value nums[j]. A triplet exists around it if something smaller appears before it and something bigger appears after it. The best candidates are simply the smallest value to its left and the largest value to its right — and both can be precomputed for every index in one pass each.
function increasingTriplet(nums) { const n = nums.length; const minLeft = [...nums]; // smallest value at or before i const maxRight = [...nums]; // largest value at or after i for (let i = 1; i < n; i++) minLeft[i] = Math.min(minLeft[i - 1], nums[i]); for (let i = n - 2; i >= 0; i--) maxRight[i] = Math.max(maxRight[i + 1], nums[i]); for (let j = 1; j < n - 1; j++) { if (minLeft[j - 1] < nums[j] && nums[j] < maxRight[j + 1]) return true; // nums[j] can be the middle } return false;}Time O(n), space O(n). Linear — but two extra arrays, and the follow-up asks for O(1) space.
Walk once and keep just two numbers:
first — the smallest value seen so far.second — the smallest value seen so far that has something smaller before it. In other words, the end of the best increasing pair.For each x: if x ≤ first, it becomes the new first. Otherwise, if x ≤ second, it becomes the new second (a better, lower pair end). Otherwise x is bigger than second — and second has a smaller value before it — so we have a triplet.
The surprising part: first can move to an index after second. That's fine. second still remembers that some smaller value came before it; the new first is just a better start for future pairs.
Try the "Tricky" example, where first jumps past second, and a strictly decreasing array, where second never gets set.
Time O(n), space O(1). One pass, two variables. The ≤ comparisons matter: with <, an array like [1, 1, 1] would wrongly report a triplet.
O(n³)Three nested loops check every i < j < k: about n³/6 triplets.
O(1)Just three indexes.
[1, 1, 1, 1] is false: the triplet must be strictly increasing.[1, 2] is false.[20, 100, 10, 12, 5, 13] is true.[5, 4, 3, 2, 1] is false.Keeping "the smallest possible end for an increasing run of length 1, 2, …" is the core of the O(n log n) Longest Increasing Subsequence algorithm — this problem is its length-3 special case.
O(n)Two passes build the min-to-the-left and max-to-the-right arrays, one more checks each middle.
O(n)Two extra arrays of length n.
O(n)One pass; each number updates first, second or ends the search in O(1).
O(1)Only two variables, whatever the input size.