Clean C++ O(n) 19ms beats 90%


  • 0
        int maxDistance(vector<vector<int>>& arrays) {
            pair<int,int>Min(INT_MAX,0);
            pair<int,int>Max(INT_MIN,0);
            pair<int,int>SecondMin(INT_MAX,0);
            pair<int,int>SecondMax(INT_MIN,0);
            for(int i=0;i<arrays.size();i++){
                if(arrays[i][0] < Min.first) 
                    SecondMin = Min, Min = make_pair(arrays[i][0],i);
                else if(arrays[i][0] < SecondMin.first)
                    SecondMin = make_pair(arrays[i][0],i);
                if(*(arrays[i].end()-1) >= Max.first)
                    SecondMax = Max, Max = make_pair(*(arrays[i].end()-1),i);
                else if(*(arrays[i].end()-1) >= SecondMax.first)
                    SecondMax = make_pair(*(arrays[i].end()-1),i);
            }
            if(Min.second != Max.second) return Max.first-Min.first;
            else return SecondMax.first-Min.first > Max.first-SecondMin.first ?
                        SecondMax.first-Min.first : Max.first-SecondMin.first;
        }
    

Log in to reply
 

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