AAlgoLoopSpaced repetition for LeetCode
MEDIUMGreedyLeetCode ↗

Increasing Triplet Subsequence

The key idea

Keep the two smallest values seen so far: 'first' (smallest) and 'second' (smallest value that has a smaller value before it). The moment any later number exceeds 'second', a valid triplet first < second < current exists. Greedily lowering 'first' is safe even after 'second' is set, because 'second' still records that some smaller value once preceded it.

Problem

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.

Constraints

Examples

Input: nums = [1,2,3,4,5] Output: true
Input: nums = [5,4,3,2,1] Output: false
Input: nums = [2,1,5,0,4,6] Output: true

Complexity

Time: O(n) Space: O(1)

See the full solution

410310
Step-by-step visualization
Start free →

More Greedy problems