> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/math/539.-minimum-time-difference.md).

# 539. Minimum Time Difference

time difference is also a circular problem, so just append data with 24hours again, it works

![](/files/MBtmsFR1T20EseSbDms3)

![](/files/2yWuMBAuLTmxJzx65bCn)

## sort + circular array

T: O(nlogn)

S: O(n)

```java
class Solution {
    public int findMinDifference(List<String> timePoints) {
        
        List<Integer> times = new ArrayList<>();
        for (String time : timePoints) {
            String hourStr = time.substring(0, 2);
            int hour = Integer.valueOf(hourStr);
            int min = Integer.valueOf(time.substring(3, 5));
            times.add(hour*60+min);
            times.add((hour+24)*60+min); // append data with 24*60 = 1440 (one more 24 hours cycle)
        }
        
        Collections.sort(times);
        
        int res = Integer.MAX_VALUE;
        for (int i = 0; i < times.size() - 1; i++) {
            res = Math.min(res, times.get(i+1) - times.get(i));
        }
        return res;
    }
}

/*
return the minimum "minutes" difference

0000
0001

2400

2259
1111


0010

2359

["00:00","23:59", 2400]

*/
```

## sort

or just calculate last one and first one case

```java
res = Math.min(res, 1440 - times.get(times.size()-1) + times.get(0));

```

```java
class Solution {
    public int findMinDifference(List<String> timePoints) {
        
        List<Integer> times = new ArrayList<>();
        for (String time : timePoints) {
            String hourStr = time.substring(0, 2);
            int hour = Integer.valueOf(hourStr);
            int min = Integer.valueOf(time.substring(3, 5));
            times.add(hour*60+min);
            // times.add((hour+24)*60+min); // append data with 24*60 = 1440 (one more 24 hours cycle)
        }
        
        Collections.sort(times);
        
        int res = Integer.MAX_VALUE;
        for (int i = 0; i < times.size() - 1; i++) {
            res = Math.min(res, times.get(i+1) - times.get(i));
        }
        
        res = Math.min(res, 1440 - times.get(times.size()-1) + times.get(0));
        return res;
    }
}
```

## bucket

T: O(n), n is size of timePoints

S: O(1)

```java
class Solution {
    public int findMinDifference(List<String> timePoints) {
        boolean[] bucket = new boolean[1440];
        int first = Integer.MAX_VALUE;
        int last = 0;
        
        for (String time : timePoints) {
            int hour = Integer.valueOf(time.substring(0, 2));
            int min = Integer.valueOf(time.substring(3, 5));
            
            int timeIdx = hour*60 + min;
            if (bucket[timeIdx]) { // if has duplicate time, means diff is 0
                return 0;
            }
            bucket[timeIdx] = true;
            first = Math.min(first, timeIdx); // record first, last time 
            last = Math.max(last, timeIdx);
        }
        
        
        int count = 0;
        int prev = 0;
        int res = Integer.MAX_VALUE;
        for (int i = 0; i < 1440; i++) {
            if (bucket[i]) { // if bucket has time
                if (count == 0) {
                    prev = i;
                }
                
                if (count != 0) {
                    res = Math.min(res, i - prev); // curr time - prev time
                    prev = i;
                }
                count++;
            }
        }
        res = Math.min(res, 1440 - last + first);
        
        return res;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/math/539.-minimum-time-difference.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
