C++ solution - 6ms - Iterative solution


  • 0
    I
    bool increasingTriplet(vector<int>& nums) {
            int n=nums.size();
            if(n <= 2)
                return false;
            int minVal=nums[0],maxVal=nums[0],maxCount=1;
            for(int i=1;i<n;i++){
                if(nums[i] > maxVal){
                    maxVal=nums[i];
                    maxCount++;
                }else{
                    if(nums[i] > minVal){
                        maxVal=nums[i];
                        maxCount=2;
                    }else{
                        minVal=nums[i];
                    }
                }
                if(maxCount == 3)
                    return true;
            }
            return false;
        }
    

    I'm just keep tracking of minimum and maximum value and whenever we get 3rd maximum , I'm returning true


Log in to reply
 

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