Java using Bucket sort


  • 0
    C

    Runtime is O(n)

        public int findMinDifference(List<String> timePoints) {
            // total number of mins per day
            boolean[] arr = new boolean[24 * 60];
            
            for(String s : timePoints){
                int min = Integer.valueOf(s.substring(0,2)) * 60;
                min += Integer.valueOf(s.substring(3));
                
                if(arr[min]){
                    return 0;
                }
                
                arr[min] = true;
            }
            
            int minDist = Integer.MAX_VALUE, beg = -1, first = beg;
            for(int i=0; i<arr.length; i++){
                if(beg == -1 && arr[i]){
                    first = i;
                    beg = i;
                }
                else if(arr[i]){
                    minDist = Math.min(minDist, i - beg);
                    beg = i;
                }
            }
            
            // this is done to find min between last and first
            minDist = Math.min(minDist, arr.length - beg + first);
            
            return minDist;
        }
    

Log in to reply
 

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