# 539.Minimum-Time-Difference

## 539. Minimum Time Difference

## 题目地址

<https://leetcode.com/problems/minimum-time-difference/>

## 题目描述

```
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list.

Example 1:
Input: ["23:59","00:00"]
Output: 1

Note:
The number of time points in the given list is at least 2 and won't exceed 20000.
The input time is legal and ranges from 00:00 to 23:59.
```

## 代码

### Approach #1 Sort

```java
class Solution {
  public int findMinDifference(List<String> timePoints) {
        int mm = Integer.MAX_VALUE;
    List<Integer> time = new ArrayList<>();

    for (int i = 0; i < timePoints.size(); i++) {
      Integer h = Integer.valueOf(timePoints.get(i).substring(0, 2));
      time.add(60 * h + Integer.valueOf(timePoints.get(i).substring(3, 5)));
    }

    Collections.sort(time, (Integer a, Integer b) -> a - b);

    for (int i = 1; i < time.size(); i++) {
      mm = Math.min(mm, time.get(i) - time.get(i - 1));
    }

    int corner = time.get(0) + (1440 - time.get(time.size() - 1));
    return Math.min(corner, mm);
  }
}
```

### Approach #2 Priority

```java
public int findMinDifference(List<String> timePoints) {
    PriorityQueue<Integer> pq = new PriorityQueue<>();
    for (String s : timePoints) {
        int h = Integer.valueOf(s.substring(0,2));
        int m = Integer.valueOf(s.substring(3));
        pq.offer(h*60+m);
    }
    if (pq.size() < 2) return 0;
    int res = Integer.MAX_VALUE, first = pq.poll();
    int cur = first;
    while (!pq.isEmpty()) {
        int next = pq.poll();
        res = Math.min(res, next-cur);
        cur = next;
    }
    return Math.min(res, 24*60 - cur + first); // difference
}
```


---

# Agent Instructions: 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://wentao-shao.gitbook.io/leetcode/array/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.
