The Indian Engineer

Problem 539 Minimum Time Difference

Posted on 5 mins

Array Math String Sorting

Problem Statement

Link - Problem 539

Question

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:

Solution

class Solution {
public:
    int findMinDifference(vector<string>& timePoints) {
        vector<int> minutes(timePoints.size());
        for (int i = 0; i < timePoints.size(); i++) {
            string time = timePoints[i];
            int h = stoi(time.substr(0, 2));
            int m = stoi(time.substr(3));
            minutes[i] = h * 60 + m;
        }
        sort(minutes.begin(), minutes.end());

        int ans = INT_MAX;
        for (int i = 0; i < minutes.size() - 1; i++) {
            ans = min(ans, minutes[i + 1] - minutes[i]);
        }

        return min(ans, 24 * 60 - minutes[minutes.size() - 1] + minutes[0]);
    }
};

Complexity Analysis

| Algorithm        | Time Complexity | Space Complexity |
| ---------------- | --------------- | ---------------- |
| Modified Sorting | O(n log n)      | O(n)             |

Explanation

Intial Thoughts

The problem can be approached by first converting all time points to a common unit, such as minutes. Then, sorting these time points can help identify the closest times. Considering the 24-hour clock, it’s also important to account for times that wrap around from the end of the day to the beginning. Key points to consider are: converting time to minutes, sorting the times, comparing adjacent times, and handling the wrap-around case. Additionally, thinking about the minimum and maximum possible differences can help guide the solution. The problem also involves understanding the concept of time and how it can be represented in a 24-hour format.

Intuitive Analysis

To intuitively solve the problem, think of the time points as points on a circle, where the circle represents the 24-hour clock. The minimum difference between two times is equivalent to finding the shortest distance between two points on the circle. This can be achieved by sorting the times and comparing adjacent points. However, to handle the wrap-around case, it’s necessary to consider the distance between the first and last points on the circle. Key insights include: recognizing that the problem involves finding the minimum distance between points on a circle, understanding how to convert time to a suitable format, and realizing the importance of considering the wrap-around case. The solution also involves using analogies, such as the circle, to simplify the problem and make it easier to understand.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.


Similar Questions:

Title URL Difficulty
Minimum Cost to Set Cooking Time https://leetcode.com/problems/minimum-cost-to-set-cooking-time Medium