Leetcode 414 Solution

This article provides solution to leetcode question 414 (third-maximum-number)

https://leetcode.com/problems/third-maximum-number

Solution

class Solution {
public:
    int thirdMax(vector<int>& nums) {
        long m1 = LONG_MIN;
        long m2 = LONG_MIN;
        long m3 = LONG_MIN;

        for (auto num : nums)
        {
            if (num > m1)
            {
                m3 = m2;
                m2 = m1;
                m1 = num;
            }
            else if (num == m1)
                continue;
            else if (num > m2)
            {
                m3 = m2;
                m2 = num;
            }
            else if (num == m2)
                continue;
            else if (num > m3)
                m3 = num;
        }

        return m3 == LONG_MIN ? m1 : m3;
    }
};