zoukankan      html  css  js  c++  java
  • leetcode 334. 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:

    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.
    

    思路:先在左边找一个最小值l(如果遇到小的就更新),如果当前值没用来更新l那么试着更新m,如果没更新m那么就存在一个符合条件的Triplet

    class Solution {
    public:
        bool increasingTriplet(vector<int>& nums) {
            if (nums.size() < 3) return false;
            int l = nums[0];
            int m = INT_MAX;
            for (int i = 1; i < nums.size(); ++i) {
                if (nums[i] <= l) l = nums[i];
                else if (nums[i] < m) m = nums[i];
                else if (nums[i] > m) return true;
            }
            return false;
        }
    };
    
  • 相关阅读:
    uniapp
    vue -element admin 修改request,headers添加参数
    uniapp
    css
    uniapp
    uniapp
    vue
    vue
    vue -element 修复select下拉框在移动端需要点击两次才能选中的问题
    vue
  • 原文地址:https://www.cnblogs.com/pk28/p/8487533.html
Copyright © 2011-2022 走看看