Problem 969
Increasing Triplet Subsequence
Medium · 20 pts · Array, Greedy
Problem
Decide whether the array has positions i < j < k with A[i] < A[j] < A[k]. Keep two values while scanning: FIRST, the smallest value seen so far, and SECOND, the smallest value that has something smaller before it. Any value larger than SECOND completes a triplet. Updating them greedily keeps both as small as possible, which only makes later completions easier, and uses O(1) extra space.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].
Output
Print YES if an increasing triplet exists, otherwise NO.
Constraints
1 <= N <= 100000; -2^31 <= A[i] <= 2^31 - 1
Example input
6 2 1 5 0 4 6
Example output
YES
Explanation
In 2 1 5 0 4 6, the values 0, 4 and 6 at positions 3, 4 and 5 are increasing, so the answer is YES.