My C++ O(n) solution


  • 11
    U
    class Solution {
        public:
            bool increasingTriplet(const vector<int>& nums) {
                int min = INT_MAX;
                int mid = INT_MAX;
                for(auto n : nums)
                {
                    if(n < min)
                    {
                        min = n;
                    }
                    else if(n > min)
                    {
                        if(mid < n)
                            return true;
                        mid = n;
                    }
                }
                return false;
            }
        };

  • 0
    M

    public class Solution {
    public boolean increasingTriplet(int[] nums) {
    int min=Integer.MAX_VALUE;
    int min2=Integer.MAX_VALUE;
    for(int i=0;i<nums.length;i++)
    {
    if(nums[i]<=min)
    min=nums[i];
    else if(nums[i]<=min2)
    min2=nums[i];
    else return true;
    }
    return false;

    }
    

    }


  • -1
    P

    Actually, it's possible to remove one condition. Just add equality to the first condition, so there is no need for the "if(n > min)" condition (else block remains the same).

    if(n <= min) {
        min = n;
    } else {
        if(mid < n)
            return true;
        mid = n;
    }
    

  • 0
    P
    This post is deleted!

Log in to reply
 

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