问题:
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.
Formally the function should:
Return true if there exists i, j, k
such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.
Your algorithm should run in O(n) time complexity and O(1) space complexity.
Examples:
Given [1, 2, 3, 4, 5]
,
return true
.
Given [5, 4, 3, 2, 1]
,
return false
.
解决:
① 要求O(n)的时间复杂度和O(1)的空间复杂度。
遍历数组,维护一个最小值,和倒数第二小值,遍历原数组的时候,如果当前数字小于等于最小值,更新最小值,如果小于等于倒数第二小值,更新倒数第二小值,如果当前数字比最小值和倒数第二小值都大,说明此时有三个递增的子序列了,直接返回ture,否则遍历结束返回false。
class Solution { //7ms
public boolean increasingTriplet(int[] nums) {
int min = Integer.MAX_VALUE;
int secmin = Integer.MAX_VALUE;
for (int n : nums){
if (min >= n){
min = n;
}else if (secmin >= n && min < n){
secmin = n;
}else{
return true;
}
}
return false;
}
}