Algorithm


Problem Name: 539. Minimum Time Differenc

Given a list of 24-hour clock time points in "HH:MM" format, return the minimum minutes difference between any two time-points in the list.

 

Example 1:

Input: timePoints = ["23:59","00:00"]
Output: 1

Example 2:

Input: timePoints = ["00:00","23:59","00:00"]
Output: 0

 

Constraints:

  • 2 <= timePoints.length <= 2 * 104
  • timePoints[i] is in the format "HH:MM".

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int findMinDifference(List timePoints) {
    List minutes = new ArrayList<>();
    for (String timePoint : timePoints) {
      minutes.add(
        Integer.parseInt(timePoint.split(":")[0]) * 60 + 
        Integer.parseInt(timePoint.split(":")[1]));
    }
    Collections.sort(minutes);
    int prev = Integer.MIN_VALUE;
    int minDiff = Integer.MAX_VALUE;
    for (int minute : minutes) {
      if (prev != Integer.MIN_VALUE) {
        minDiff = Math.min(minDiff, minute - prev);
      }
      prev = minute;
    }
    minDiff = Math.min(minDiff, (24 * 60 - minutes.get(minutes.size() - 1) + minutes.get(0)));
    return minDiff;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
timePoints = ["23:59","00:00"]

Output

x
+
cmd
1

#2 Code Example with Javascript Programming

Code - Javascript Programming


const findMinDifference = function(timePoints) {
  const sortedArr = timePoints
    .map(el => {
      const arr = el.trim().split(":");
      return arr[0] === "00" && arr[1] === "00"
        ? 24 * 60
        : +arr[0] * 60 + +arr[1];
    })
    .sort((a, b) => a - b);
  let prev = sortedArr[0];
  let res = Number.MAX_SAFE_INTEGER;
  const mid = 12 * 60;
  for (let i = 1; i  <  sortedArr.length; i++) {
    res = Math.min(res, Math.abs(sortedArr[i] - prev));
    prev = sortedArr[i];
  }
  if (sortedArr[0]  <  mid && sortedArr[sortedArr.length - 1] > mid) {
    res = Math.min(
      res,
      sortedArr[0] + 2 * mid - sortedArr[sortedArr.length - 1]
    );
  }
  return res;
};

console.log(findMinDifference(["23:59", "00:00"]));
console.log(findMinDifference(["12:12", "00:13"]));
console.log(findMinDifference(["05:31", "22:08", "00:35"]));
Copy The Code & Try With Live Editor

Input

x
+
cmd
timePoints = ["23:59","00:00"]

Output

x
+
cmd
1

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def findMinDifference(self, tp):
        def getMinute(t): 
            h , m = t.split(":")
            return int(h) * 60 + int(m)
        tp = sorted(map(getMinute, tp))
        mn = sys.maxsize
        for i in range(len(tp) - 1): 
            mn = min(mn, tp[i + 1] - tp[i])
            if mn == 0: return 0
        return min(mn, 1440 + tp[0] - tp[-1])
Copy The Code & Try With Live Editor

Input

x
+
cmd
timePoints = ["00:00","23:59","00:00"]
Advertisements

Demonstration


Previous
#538 Leetcode Convert BST to Greater Tree Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#540 Leetcode Single Element in a Sorted Array Solution in C, C++, Java, JavaScript, Python, C# Leetcode