Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.
Formally the function should:
Note: Your algorithm should run in O(n) time complexity and O(1) space complexity.
Example
Example 1:
Example 2:
Note
常数个数版本单调栈,主要体会一下思路
通过观察这题的具体性质,我们发现在这里我们所谓的 "单调栈" 长度其实是固定的,就是3,等于3了直接返回就行。
因为 3 非常小,我们只需要存两个值,分别代表着单调栈的第一个和第二个位置;当我们碰到第三个位置的情况,就可以返回 true 了。
Code
Last updated