Posted on

Description

Submission

class Solution {
public:
    int thirdMax(vector<int>& nums) {
        long max1 = LONG_MIN, max2 = LONG_MIN, max3 = LONG_MIN;

        for(long num : nums) {
            if(num == max1 || num == max2 || num == max3) continue;
            if(num > max1) {
                max3 = max2;
                max2 = max1;
                max1 = num;
            } else if(num > max2) {
                max3 = max2;
                max2 = num;
            } else if(num > max3) {
                max3 = num;
            }
        }

        if(max3 == LONG_MIN) return max1;
        return max3;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *