Increasing Triplet Subsequence

Given an integer array nums, return true if there are indices i < j < k with nums[i] < nums[j] < nums[k]. If no such three indices exist, return false.

30:00

Increasing Triplet Subsequence
medium
Free written solution
Topics
Companies

Given an integer array nums, return true if there are indices i < j < k with nums[i] < nums[j] < nums[k]. If no such three indices exist, return false.

Example 1:
Input: {"nums":[1,2,3,4,5]}
Output: true
Constraints:
  • 1≤nums.length≤5×1051 \leq \text{nums.length} \leq 5 \times 10^5

  • −231≤nums[i]≤231−1-2^{31} \leq \text{nums}[i] \leq 2^{31} - 1

  • Follow up: can you solve it in O(n)O(n) time and O(1)O(1) extra space?

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,2,3,4,5]}

first and second start at infinity. first is the smallest value seen; second is the smallest value that has something smaller before it.

nums

1

0

2

1

3

2

4

3

5

4

Three rising steps

inf

smallest

first

inf

needs a smaller one

second

?

> second wins

third

Variables
VariableValue
firstinf
secondinf
DepthFunction Call
Stack empty
0/3