C++ Solution 3 passes to track each - no sets.


  • 0
    S

    Three passes track each ignore first two and return. Use long long to bypass int boundary problems.

    int thirdMax(vector<int> nums) {
        long long fmax = LLONG_MIN, smax = LLONG_MIN, tmax = LLONG_MIN;
        for (int x : nums) fmax = std::max<long long>(fmax, x);
        for (int y : nums) if (y != fmax) smax = std::max<long long>(smax, y);
        for (int z : nums) if (z != smax && z != fmax) tmax = std::max<long long>(tmax, z);
    
        return (tmax == LLONG_MIN) ? fmax : tmax;
    }

Log in to reply
 

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