O(N) solution in C++. O(1) space Complexity. Cascading solution - how a third grader would solve this


  • 0
    P
    class Solution {
    public:
        int thirdMax(vector<int>& nums) {
    
            long max1 = LONG_MIN;
            long max2 = LONG_MIN;
            long max3 = LONG_MIN;
    
            for(auto num : nums) {
                if (num > max3 && num!=max2 && num!= max1) {
                    max3 = num;
                    if (max3 > max2) {
                        swap(max2,max3);
                        if(max2 > max1)
                            swap(max1,max2);
                    }
                }
            }
    
            if (max3 == LONG_MIN)
                max3 = max1;
            return max3;
        }
    };
    

Log in to reply
 

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