Java 10 liner solution. Simplest so far


  • 7
    H
    public class Solution {
        public int findMinDifference(List<String> timePoints) {
            int mm = Integer.MAX_VALUE;
            List<Integer> time = new ArrayList<>();
            
            for(int i = 0; i < timePoints.size(); i++){
                Integer h = Integer.valueOf(timePoints.get(i).substring(0, 2));
                time.add(60 * h + Integer.valueOf(timePoints.get(i).substring(3, 5)));
            }
            
            Collections.sort(time, (Integer a, Integer b) -> a - b);
            
            for(int i = 1; i < time.size(); i++){
                System.out.println(time.get(i));
                mm = Math.min(mm, time.get(i) - time.get(i-1));
            }
            
            int corner = time.get(0) + (1440 - time.get(time.size()-1));
            return Math.min(mm, corner);
        }
    }

  • 0
    H
    public int findMinDifference(List<String> timePoints) {
            int mm = Integer.MAX_VALUE;
            List<Integer> time = new ArrayList<>();
            
            for(int i = 0; i < timePoints.size(); i++){
                Integer h = Integer.valueOf(timePoints.get(i).substring(0, 2));
                time.add(60 * h + Integer.valueOf(timePoints.get(i).substring(3, 5)));
            }
            
            Collections.sort(time, (Integer a, Integer b) -> a - b);
            for(int i = 1; i < time.size(); i++) mm = Math.min(mm, time.get(i) - time.get(i-1));    
            int corner = time.get(0) + (1440 - time.get(time.size()-1)); //1440 = 24*60
    
            return Math.min(mm, corner);
        }

  • 1
    B

    A similar solution with a little bit more Java 8.

    public class Solution {
      public int findMinDifference(List<String> timePoints) {
        int[] minutes = timePoints.stream().mapToInt((s) -> {
          String[] value = s.split(":");
          return 60 * Integer.parseInt(value[0]) + Integer.parseInt(value[1]);
        }).sorted().toArray();
    
        int min = IntStream.range(0, minutes.length - 1)
            .map(i -> minutes[i + 1] - minutes[i])
            .min()
            .getAsInt();
    
        int corner = minutes[0] + (1440 - minutes[minutes.length - 1]);
        return Math.min(min, corner);
      }
    }
    

  • 0
    public int findMinDifference(List<String> list) {
        if(list == null || list.size() == 0) return 0;
        int[] min = new int[] {Integer.MAX_VALUE};
        Collections.sort(list, (a, b) -> {min[0] = Math.min(diff(a, b, 0), min[0]); return a.compareTo(b);});
        return Math.min(min[0], diff(list.get(0), list.get(list.size() - 1), 24 * 60));
    }
    private int diff(String a, String b, int extraMins) {
        String[] aa = a.split(":"), bb = b.split(":");
        int minA = Integer.parseInt(aa[0]) * 60 + Integer.parseInt(aa[1]) + extraMins;
        int minB = Integer.parseInt(bb[0]) * 60 + Integer.parseInt(bb[1]);
        return Math.abs(minA - minB);
    }

  • 0

    @bin01 Lovely solution


Log in to reply
 

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