Shortcut?


  • 0
    T

    Its basically a brute force technique, but when the array is longer than 1440, you can simply return 0. Ill leave it to you to think about it.

    class Solution {
        public int findMinDifference(List<String> timePoints) {
            
            if(timePoints.size() > 1440) return 0;
    
            int min = 1440;
            for(int i = 0; i < timePoints.size(); i++) {
                for(int j = 0; j < timePoints.size(); j++) {
                    if( i != j ) {
                        int temp = getDifference(timePoints.get(i), timePoints.get(j));
                        if(min > temp) {
                            min = temp;
                        }
                        if(temp == 0) return 0;
                    }
                }
            }
            
            return min;
            
        }
        
        public int getDifference(String t1, String t2) {
            int min1 = getMinutes(t1);
            int min2 = getMinutes(t2);
            int diff1 = Math.abs(min1 - min2);
            int diff2 = 1440 - diff1;
            return (diff1 > diff2) ? diff2 : diff1;
        }
        
        private int getMinutes(String t) {
            String arr[] = t.split(":");
            return Integer.parseInt(arr[0]) * 60 + Integer.parseInt(arr[1]);
        }
    }
    

Log in to reply
 

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