convert time to seconds and add a sentinel to handle around clock.


  • 0
    Z

    My original string based implementation is sort of in place, but super slow (~200ms), the new one based on seconds is more straightforward and faster than the string based.

    public int findMinDifference(List<String> timePoints) {
        List<Integer> times = new ArrayList<>();
        for (String time: timePoints) {
            String[] hm = time.split(":");
            times.add(Integer.parseInt(hm[0]) * 60 + Integer.parseInt(hm[1]));
        }
    
        Collections.sort(times);
        times.add(times.get(0) + 24 * 60);
    
        int min = Integer.MAX_VALUE;
        int n = times.size();
        for (int i = 1; i < n; i++){
            min = Math.min(min, times.get(i) - times.get(i - 1));
        }
        return min;
    }

Log in to reply
 

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