Topic:
Given an unordered array, determine if there is an incrementing subsequence of length 3 in this array. The formal mathematical expression is as follows: if there are such I, J, K, and satisfy 0≤i < J < K≤n-1, make Arr[i] < Arr[j] < Arr[k], return true; The time complexity of the algorithm is O (n) and the space complexity is O (1). Example: input [1, 2, 3, 4, 5], output true. Input [5, 4, 3, 2, 1], output false.
Solution:
Class Solution {public: bool Increasingtriplet (vector<int>& nums) { int len = Nums.size (); if (Len <= 2) { return false; } Vector<int> Minleft (len,nums[0]); Vector<int> maxright (len,nums[len-1]); for (int i = 1;i < Len;++i) { Minleft[i] = min (nums[i],minleft[i-1]); } for (int i = Len-2;i >= 0;--i) { maxright[i] = max (nums[i],maxright[i+1]); } for (int i = 1; i < len-1; ++i) { if (Minleft[i-1] < nums[i] && Nums[i] < maxright[i+1]) { return T Rue; } } return false; }};
"Intermediate algorithm" 5. Incrementing ternary subsequence