Simple JavaScript O(n) solution


  • 0
    var maxDistance = function(arrays) {
        const maxes = [-Infinity, -Infinity];
        for (let arr of arrays) {
            maxes.push(arr[arr.length - 1]);
            maxes.sort((a, b) => a - b).shift();
        }
        
        let res = -Infinity;
        for (let arr of arrays) {
            let max = arr[arr.length - 1];
            if (max === maxes[0]) {
                res = Math.max(res, maxes[1] - arr[0]);
            } else if (max === maxes[1]) {
                res = Math.max(res, maxes[0] - arr[0]);
            } else {
                res = Math.max(res, maxes[0] - arr[0], maxes[1] - arr[0]);
            }
        }
        
        return res;
    };
    

    Find the two largest, then use them to find the max absolute difference not in the same array.


Log in to reply
 

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