4 lines C++....


  • 1

    The simple one is already taken, so I feel forced to post tricky ones :-D

    bool increasingTriplet(vector<int>& nums) {
        int inc[3] = {INT_MAX, INT_MAX, INT_MIN};
        for (int x : nums)
            inc[(x > inc[0]) + (x > inc[1])] = x;
        return inc[2] > inc[1];
    }
    

    Or:

    bool increasingTriplet(vector<int>& nums) {
        int a = INT_MAX, b = a, c = INT_MIN;
        for (int x : nums)
            (x > b ? c : x > a ? b : a) = x;
        return c > b;
    }
    

    Or:

    bool increasingTriplet(vector<int>& nums) {
        int inc[3] = {INT_MAX, INT_MAX, INT_MIN};
        for (int x : nums)
            *lower_bound(inc, inc + 2, x) = x;
        return inc[2] > inc[1];
    }
    

    Though for completeness, here's my original:

    bool increasingTriplet(vector<int>& nums) {
        int a = INT_MAX, b = a;
        for (int x : nums) {
            if (x <= a)
                a = x;
            else if (x <= b)
                b = x;
            else
                return true;
        }
        return false;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.