C++ O(n) solution easy

interview-questions
Tags: #<Tag:0x00007f2434c767a8>

#1
int Solution::maxp3(vector<int> &nums) {
    if(nums.size()<3)
        return 0;
    int f=INT_MIN, s= INT_MIN, t= INT_MIN;
        int m1=INT_MAX,m2=INT_MAX;
        for(int i=0; i<nums.size();i++){
            if(nums[i] >= f){
                t=s;
                s=f;
                f=nums[i];
            }
            else if(nums[i] >= s){
                t=s;
                s=nums[i];
            }
            else if(nums[i] >=t)
                t=nums[i];
            if(nums[i]<= m1){
                m2=m1;
                m1= nums[i];
            }
            else if(nums[i] <= m2)
                m2=nums[i];
        }
        
        return max(f*t*s, f*m1*m2);
}