539. Minimum Time Difference LeetCode Solution

In this guide, you will get 539. Minimum Time Difference LeetCode Solution with the best time and space complexity. The solution to Minimum Time Difference problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Time Difference solution in C++
  4. Minimum Time Difference solution in Java
  5. Minimum Time Difference solution in Python
  6. Additional Resources
539. Minimum Time Difference LeetCode Solution image

Problem Statement of Minimum Time Difference

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".

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(24 \cdot 60)

539. Minimum Time Difference LeetCode Solution in C++

class Solution {
 public:
  int findMinDifference(vector<string>& timePoints) {
    int ans = 24 * 60;
    int first = 24 * 60;
    vector<bool> bucket(24 * 60);

    for (const string& time : timePoints) {
      const int num = stoi(time.substr(0, 2)) * 60 + stoi(time.substr(3));
      first = min(first, num);
      if (bucket[num])
        return 0;
      bucket[num] = true;
    }

    int prev = first;

    for (int i = first + 1; i < bucket.size(); ++i)
      if (bucket[i]) {
        ans = min(ans, i - prev);
        prev = i;
      }

    return min(ans, 24 * 60 - prev + first);
  }
};
/* code provided by PROGIEZ */

539. Minimum Time Difference LeetCode Solution in Java

class Solution {
  public int findMinDifference(List<String> timePoints) {
    int ans = 24 * 60;
    int first = 24 * 60;
    boolean[] bucket = new boolean[24 * 60];

    for (final String timePoint : timePoints) {
      final int num =
          Integer.valueOf(timePoint.substring(0, 2)) * 60 + Integer.valueOf(timePoint.substring(3));
      first = Math.min(first, num);
      if (bucket[num])
        return 0;
      bucket[num] = true;
    }

    int prev = first;

    for (int i = first + 1; i < bucket.length; ++i)
      if (bucket[i]) {
        ans = Math.min(ans, i - prev);
        prev = i;
      }

    return Math.min(ans, 24 * 60 - prev + first);
  }
}
// code provided by PROGIEZ

539. Minimum Time Difference LeetCode Solution in Python

class Solution:
  def findMinDifference(self, timePoints: list[str]) -> int:
    ans = 24 * 60
    nums = sorted([int(timePoint[:2]) * 60 + int(timePoint[3:])
                   for timePoint in timePoints])

    for a, b in zip(nums, nums[1:]):
      ans = min(ans, b - a)

    return min(ans, 24 * 60 - nums[-1] + nums[0])
# code by PROGIEZ

Additional Resources

See also  3065. Minimum Operations to Exceed Threshold Value I LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.