Java, faster than 97% of submissions


  • 0
    K
    public class Solution {
        public int findMinDifference(List<String> timePoints) {
            boolean[] times = new boolean[1440];
            for (String timeString : timePoints) {
                int minutes = convertToMinutes(timeString);
                if (times[minutes]) {
                    return 0;
                }
                times[minutes] = true;
            }
            
            int minimum = Integer.MAX_VALUE;
            int first = -1;
            int prev = -1;
            for (int i = 0; i < times.length; i ++) {
                if (!times[i]) {
                    continue;
                }
                
                if (prev == -1) {
                    prev = i;
                    first = i;
                    continue;
                }
                
                int distance = i - prev;
                distance = Math.min(1440 - distance, distance);
                minimum = Math.min(minimum, distance);
                prev = i;
            }
            
            // take the difference of first and last
            minimum = Math.min(minimum, first + 1440 - prev);
            
            return minimum;
        }
        
        private int convertToMinutes(String timeString) {
            return (60 * Integer.valueOf(timeString.substring(0, 2))) + Integer.valueOf(timeString.substring(3));
        }
    }
    

Log in to reply
 

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