Java 13ms solution, beats 98%.


  • 0
    G

    Refers to @shawngao. If the input size large than 1440, we can directly return 0.

    public int findMinDifference(List<String> timePoints) {
        int MAX = 24 * 60;
        if (timePoints.size() > MAX) {
          return 0;
        }
        boolean[] mark = new boolean[MAX];
        for (String time : timePoints) {
          int h = Integer.valueOf(time.substring(0, 2));
          int m = Integer.valueOf(time.substring(3));
          if (mark[h * 60 + m]) {
            return 0;
          }
          mark[h * 60 + m] = true;
        }
        int prev = -1, first = -1;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < MAX; i++) {
          if (mark[i]) {
            if (prev != -1) {
              min = Math.min(min, i - prev);
              if (min == 1)
                return min;
            } else {
              first = i;
            }
            prev = i;
          }
        }
        min = Math.min(min, MAX - prev + first);
        return min;
      }
    

Log in to reply
 

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