DSA problem
Increasing Triplet Subsequence
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 exist, return false.
This is a classic greedy problem. A brute-force approach checks every triple in O(n^3) time, which is far too slow for large inputs. The optimal solution scans the array once while tracking two values: the smallest element seen so far (first) and the smallest element that has a strictly smaller predecessor (second).
If at any point we encounter a number strictly greater than second, we have found a valid increasing triplet and can return true immediately.
Examples
nums = [1,2,3,4,5]truenums = [5,4,3,2,1]falsenums = [2,1,5,0,4,6]trueConstraints
- 1 <= nums.length <= 5 * 10^5
- -2^31 <= nums[i] <= 2^31 - 1
3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.