<p>Given an integer array <code>nums</code>, return <em>the <strong>third distinct maximum</strong> number in this array. If the third maximum does not exist, return the <strong>maximum</strong> number</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,2,1]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
The first distinct maximum is 3.
The second distinct maximum is 2.
The third distinct maximum is 1.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The first distinct maximum is 2.
The second distinct maximum is 1.
The third distinct maximum does not exist, so the maximum (2) is returned instead.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,2,3,1]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
The first distinct maximum is 3.
The second distinct maximum is 2 (both 2&#39;s are counted together since they have the same value).
The third distinct maximum is 1.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
	<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

<p>&nbsp;</p>
<strong>Follow up:</strong> Can you find an <code>O(n)</code> solution?
class Solution {
private:
    std::vector<int> maxNums;
    int count;
    void appendMax(int num)
    {
        switch(count)
        {
            case 0:
                maxNums[0] = num;
                count++;
                break;
            case 1:
                if (maxNums[0] != num)
                {
                    maxNums[1] = std::max(num, maxNums[0]);
                    maxNums[0] = std::min(num, maxNums[0]);
                    count++;
                }
                break;
            case 2:
                if ((maxNums[1] != num) && (maxNums[0] != num))
                {
                    maxNums[2] = std::max(num, maxNums[1]);
                    maxNums[1] = std::min(num, maxNums[1]);
                    if (maxNums[1] < maxNums[0])
                    {
                        std::swap(maxNums[1], maxNums[0]);
                    }
                    count++;
                }
                break;
            case 3:
                if ((maxNums[2] != num) && (maxNums[1] != num) && (maxNums[0] != num))
                {
                    for (int i = 2; i >= 0; i--)
                    {
                        if (maxNums[i] < num)
                        {
                            std::swap(maxNums[i], num);
                        }
                    }
                }
                break;
            default:
        }
    }
public:
    int thirdMax(vector<int>& nums) {
        maxNums.resize(3, INT_MIN);
        count = 0;

        for (int n : nums)
        {
            appendMax(n);
            std::cout << maxNums[2] << " " << maxNums[1] << " " << maxNums[0] << "\n";
        }

        if (count == 3)
        {
            return maxNums[0];
        }
        return maxNums[count-1];
    }
};
13000