Java solution (easy to understand)


  • 0
    L
    public class Solution {
        public int findMinDifference(List<String> timePoints) {
            int allMinutes = 24*60;
            ArrayList<Integer> list = new ArrayList<Integer>();
            for (String s : timePoints) {
                String[] t = s.split(":");
                int h = Integer.parseInt(t[0]);
                int m = Integer.parseInt(t[1]);
                int temp = h*60 + m;
                list.add(temp);
            }
            Integer[] arr = new Integer[list.size()];
            for (int i = 0; i < list.size(); i++) {
                arr[i] = list.get(i);
            }
            Arrays.sort(arr);
            int result = Integer.MAX_VALUE;
            for (int i = 0; i < arr.length - 1; i++) {
                int tempMin = arr[i+1] - arr[i];
                result = Math.min(result,tempMin);
            }
            int min = arr[0];
            int max = arr[arr.length - 1];
            result = Math.min(result,allMinutes - max + min);
            return result;
        }
    }
    

Log in to reply
 

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