Java Solution using sorting O(n logn)


  • 0
    V
    class Solution {
        public int findMinDifference(List<String> timePoints) {
            Collections.sort(timePoints);
            int minDiff = Integer.MAX_VALUE;
            for (int i = 1; i < timePoints.size(); i++) {
                minDiff = Math.min(getDiff(timePoints.get(i - 1), timePoints.get(i)), minDiff);
            }
            
            if (timePoints.size() > 1) {
                minDiff = Math.min(getDiff(timePoints.get(0), timePoints.get(timePoints.size() - 1)), minDiff);
            }
            
            return minDiff;
        }
        
        private int getDiff(String t1, String t2) {
            int time1 = Integer.parseInt(t1.split(":")[0]) * 60 + Integer.parseInt(t1.split(":")[1]);
            int time2 = Integer.parseInt(t2.split(":")[0]) * 60 + Integer.parseInt(t2.split(":")[1]);
            
            return Math.min(Math.abs(time1 - time2), 1440 - Math.abs(time1 - time2));
        }
    }
    

Log in to reply
 

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