Java Solution using Sorting


  • 0
    U
    public class Solution {
        public int findMinDifference(List<String> timePoints) {
            
            int min = Integer.MAX_VALUE;
            
            if(timePoints == null || timePoints.size() ==0) {
                return min;
            }
            
            ArrayList <Integer> minutes = new ArrayList();
            // Convert TimePoints String to Minutes Integer List
            for(int i=0; i<timePoints.size(); i++) {
                String str = timePoints.get(i);
                String[] mins = str.split(":");
                Integer res = (Integer.valueOf(mins[0]) * 60) + Integer.valueOf(mins[1]); 
                minutes.add(res);
            }
            // Sort the Integer array
            Collections.sort(minutes);
            // To avoid difference between 23:59 , 00:10, 00.25 => min should be 11 not 15 mins. so add 24*60 of first element
            Integer earliest = Integer.valueOf(minutes.get(0))+24*60;
            minutes.add(earliest);
            // Find min difference
            for(int i=1; i<minutes.size(); i++) {
                min = Math.min(min, minutes.get(i)-minutes.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.