334. 递增的三元子序列

https://leetcode-cn.com/problems/increasing-triplet-subsequence/

https://leetcode-cn.com/problems/increasing-triplet-subsequence/solution/c-z-by-zrita-20/

class Solution {
public:
    bool increasingTriplet(vector<int>& nums) {
        int first = INT_MAX, sec = INT_MAX;     
        for(int n : nums)                       //
        {
            if(n <= first)  first = n;          
            else
            {
                if(n <= sec )   sec = n;        
                else   return true;
            }
        }
        return false;

    }
};

class Solution {
public:
    bool increasingTriplet(vector<int>& nums) {
        if (nums.size() < 3) return false;
        int size = nums.size();
        vector<int> f(size, nums[0]), b(size, nums.back());
        for (int i = 1; i < size; ++i) {
            f[i] = min(f[i - 1], nums[i]);
        }
        for (int i = size - 2; i >= 0; --i) {
            b[i] = max(b[i + 1], nums[i]);
        }
        for (int i = 0; i < size; ++i) {
            if (f[i] < nums[i] && nums[i] < b[i]) return true;
        }
        return false;
    }
};


posted @ 2020-07-28 22:21  aaaaassss  阅读(59)  评论(0编辑  收藏  举报