[C++] Solution


  • 0
    /**
     * find the array id of min and max, if they are not from the same array, return diff;
     * otherwise, for every other array calculate their min with the global max, and their max with global min;
     */
    class Solution {
    public:
        int maxDistance(vector<vector<int>>& aa) {
            int minid = 0;
            int maxid = 0;
            for (int i = 0; i < aa.size(); i++) {
                minid = aa[minid].front() <= aa[i].front() ? minid : i;
                maxid = aa[maxid].back() >= aa[i].back() ? maxid : i;
            }
            if (minid != maxid) {
                return aa[maxid].back() - aa[minid].front();
            }
            
            int maxdist = INT_MIN;
            for (int i = 0; i < aa.size(); i++) {
                if (i != minid) {
                    maxdist = max(maxdist, aa[i].back() - aa[minid].front());
                    maxdist = max(maxdist, aa[maxid].back() - aa[i].front());
                }
            }
            return maxdist;
        }
    };
    

Log in to reply
 

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