comments | difficulty | edit_url | tags | ||||
---|---|---|---|---|---|---|---|
true |
Medium |
|
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".
We notice that there can be at most
Next, we iterate through the list of time points and convert it into a list of minutes 13:14
, we convert it into
Then, we sort the list of minutes in ascending order and append the smallest time
Finally, we iterate through the list of minutes to find the minimum difference between any two adjacent times.
The time complexity is
class Solution:
def findMinDifference(self, timePoints: List[str]) -> int:
if len(timePoints) > 1440:
return 0
nums = sorted(int(x[:2]) * 60 + int(x[3:]) for x in timePoints)
nums.append(nums[0] + 1440)
return min(b - a for a, b in pairwise(nums))
class Solution {
public int findMinDifference(List<String> timePoints) {
if (timePoints.size() > 1440) {
return 0;
}
int n = timePoints.size();
int[] nums = new int[n + 1];
for (int i = 0; i < n; ++i) {
String[] t = timePoints.get(i).split(":");
nums[i] = Integer.parseInt(t[0]) * 60 + Integer.parseInt(t[1]);
}
Arrays.sort(nums, 0, n);
nums[n] = nums[0] + 1440;
int ans = 1 << 30;
for (int i = 1; i <= n; ++i) {
ans = Math.min(ans, nums[i] - nums[i - 1]);
}
return ans;
}
}
class Solution {
public:
int findMinDifference(vector<string>& timePoints) {
if (timePoints.size() > 1440) {
return 0;
}
int n = timePoints.size();
vector<int> nums(n + 1);
for (int i = 0; i < n; ++i) {
int hours = stoi(timePoints[i].substr(0, 2));
int minutes = stoi(timePoints[i].substr(3, 2));
nums[i] = hours * 60 + minutes;
}
sort(nums.begin(), nums.begin() + n);
nums[n] = nums[0] + 1440;
int ans = INT_MAX;
for (int i = 1; i <= n; ++i) {
ans = min(ans, nums[i] - nums[i - 1]);
}
return ans;
}
};
func findMinDifference(timePoints []string) int {
if len(timePoints) > 1440 {
return 0
}
n := len(timePoints)
nums := make([]int, n+1)
for i, time := range timePoints {
parts := strings.Split(time, ":")
hours, _ := strconv.Atoi(parts[0])
minutes, _ := strconv.Atoi(parts[1])
nums[i] = hours*60 + minutes
}
sort.Ints(nums[:n])
nums[n] = nums[0] + 1440
ans := 1 << 30
for i := 1; i <= n; i++ {
ans = min(ans, nums[i]-nums[i-1])
}
return ans
}
function findMinDifference(timePoints: string[]): number {
if (timePoints.length > 1440) {
return 0;
}
const n = timePoints.length;
const nums: number[] = Array(n + 1);
for (let i = 0; i < n; ++i) {
const [hours, minutes] = timePoints[i].split(':').map(Number);
nums[i] = hours * 60 + minutes;
}
nums.sort((a, b) => a - b);
nums[n] = nums[0] + 1440;
let ans = 1 << 30;
for (let i = 1; i <= n; ++i) {
ans = Math.min(ans, nums[i] - nums[i - 1]);
}
return ans;
}
impl Solution {
pub fn find_min_difference(time_points: Vec<String>) -> i32 {
if time_points.len() > 1440 {
return 0;
}
let n = time_points.len();
let mut nums: Vec<i32> = Vec::with_capacity(n + 1);
for time in time_points.iter() {
let parts: Vec<i32> = time.split(':').map(|s| s.parse().unwrap()).collect();
let minutes = parts[0] * 60 + parts[1];
nums.push(minutes);
}
nums.sort();
nums.push(nums[0] + 1440);
let mut ans = i32::MAX;
for i in 1..=n {
ans = ans.min(nums[i] - nums[i - 1]);
}
ans
}
}
class Solution {
func findMinDifference(_ timePoints: [String]) -> Int {
if timePoints.count > 1440 {
return 0
}
var nums = [Int]()
for t in timePoints {
let time = t.split(separator: ":").map { Int($0)! }
nums.append(time[0] * 60 + time[1])
}
nums.sort()
nums.append(nums[0] + 1440)
var ans = Int.max
for i in 1..<nums.count {
ans = min(ans, nums[i] - nums[i - 1])
}
return ans
}
}