Concise JavaScript O(n log n) using lexicographical sort and Date objects


  • 0
    var findMinDifference = function(timePoints) {
        timePoints.sort();
        let prev = new Date('2017-03-29T' + timePoints[0]);
        let min = prev - new Date('2017-03-28T' + timePoints[timePoints.length - 1]);
        for (let i = 1; i < timePoints.length; i++) {
            let curr = new Date('2017-03-29T' + timePoints[i]);
            min = Math.min(min, curr - prev);
            prev = curr;
        }
        return min / 60000;
    };
    

    The string sort allows us to use just one loop since we don't need to parse times to know their order.


Log in to reply
 

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