← All problems

8. Increasing Triplet Subsequence

MediumArray / StringOriginal on LeetCode ↗

Return True if there are positions i < j < k with nums[i] < nums[j] < nums[k], and False otherwise. The three numbers don't need to be next to each other.

Try for O(n) time and O(1) extra space.

Constraints

  • 1 <= len(nums) <= 500000
  • -2^31 <= nums[i] <= 2^31 - 1

Examples

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

Hints

Hint 1

Keep the smallest value seen so far, and the smallest value that has something smaller before it.

Hint 2

Any number bigger than that second value completes a triplet.

Python 3

Run checks the examples · Submit checks every test. The first run downloads Python (about 10 MB) and takes a few seconds.