C++ 4ms solution, similar to 3Sum


  • 1
    G
    class Solution {
    public:
        bool increasingTriplet(vector<int>& nums) {
            int n = nums.size();
            if(n < 3) return false;
            for(int i = 1; i < n - 1; i++){
                bool left = false;
                for(int j = 0; j < i; j++){
                    if(nums[j] < nums[i]){
                        left = true;
                        break;
                    }
                }
    
                if(left){
                    for(int k = n - 1; k > i; k--){
                        if(nums[k] > nums[i]){
                            return true;
                        }
                    }
                }
                else{ 
                    //if left == false, then we can not find  a smaller value on the left
                    // move i to right
                    while(i + 1 < n && nums[i] >= nums[i + 1]){
                        i++;
                    }
                }
            }
            return false;
        }
    };

Log in to reply
 

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