LeetCode Problem

How to Solve Minimum Time Difference

To solve Minimum Time Difference, convert all time strings to minutes and sort the resulting array. Compare consecutive differences and include the circular case from last to first to ensure the absolute minimum. This approach leverages array sorting and modular arithmetic to guarantee correctness while maintaining optimal performance for large inputs.

GhostInterview Help

Need help with Minimum Time Difference without spending extra time grinding it?

GhostInterview can read Minimum Time Difference from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #539Array plus MathReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Array plus Math
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve Minimum Time Difference, convert all time strings to minutes and sort the resulting array. Compare consecutive differences and include the circular case from last to first to ensure the absolute minimum. This approach leverages array sorting and modular arithmetic to guarantee correctness while maintaining optimal performance for large inputs.

Problem Statement

Given a list of time points in 24-hour "HH:MM" format, find the minimum difference in minutes between any two times. The difference is measured on a circular clock, meaning the day wraps from 23:59 to 00:00.

Return the smallest difference in minutes. Each time point is guaranteed to be a valid 24-hour format, and duplicates may appear, making zero the minimum in some cases.

Examples

Example 1

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

Output: 1

Example details omitted.

Example 2

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

Output: 0

Example details omitted.

Constraints

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

Solution Approach

Convert Times to Minutes

Iterate through the array of time strings, split each into hours and minutes, and convert each to total minutes. This allows straightforward numerical comparison instead of string manipulation.

Sort and Compute Differences

Sort the array of minute values, then compute differences between consecutive elements. Track the minimum difference found in this linear pass, which identifies the smallest gap efficiently.

Handle Circular Difference

Include the difference between the last and first element across midnight using modulo 1440 to account for the day wrap-around. This ensures the circular nature of time is respected and no minimal gap is missed.

Complexity Analysis

MetricValue
TimeO(N)
SpaceO(1)

Time complexity is O(N) for converting and sorting if bucket counting is used, otherwise O(N log N) with standard sort. Space complexity is O(1) aside from the array of minutes since in-place computations are possible.

What Interviewers Usually Probe

  • Asks about handling duplicates and zero-minute differences.
  • Checks awareness of circular time calculation and modulo logic.
  • Tests for edge cases like adjacent midnight values.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting the wrap-around from 23:59 to 00:00 and missing minimal differences.
  • Incorrectly comparing string times instead of numeric minutes.
  • Not handling duplicate times which can produce zero difference.

Follow-up variants

  • Compute maximum time difference instead of minimum.
  • Allow times with seconds and compute minimal difference including seconds.
  • Find k smallest differences among all time points.

How GhostInterview Helps

  • Automatically converts time strings to numeric values for immediate difference computation.
  • Detects edge cases with duplicates and circular wrap-around to prevent incorrect answers.
  • Provides step-by-step comparison between sorted times to quickly identify minimal gaps.

Topic Pages

FAQ

What is the main approach for Minimum Time Difference?

Convert all time strings to minutes, sort the array, then check consecutive differences including the circular midnight case.

Can timePoints contain duplicates?

Yes, duplicates are allowed and can result in a minimum difference of zero.

Why do we need to consider circular differences?

Because the clock wraps from 23:59 to 00:00, ignoring this can miss the true minimal difference.

What is the optimal time complexity?

Using bucket sort or counting sort, the problem can be solved in O(N) time, otherwise standard sorting gives O(N log N).

Which pattern does this problem follow?

This is an Array plus Math pattern where array manipulation and numeric calculations are combined for efficient solution.

GhostInterview Solver

Need direct help with Minimum Time Difference instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Minimum Time Difference from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.