Welcome to Subscribe On Youtube
539. Minimum Time Difference
Description
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".
Solutions
- Convert all the times into minutes values
mins
, i.e.13:14
=>13 * 60 + 14
. - Sort the
mins
. - Push the time
mins[0] + 24 * 60
to deal with min and max diff. - For each value in
mins[1:]
, calculate the min diffmins[i] - mins[i - 1]
.
-
class Solution { public int findMinDifference(List<String> timePoints) { if (timePoints.size() > 24 * 60) { return 0; } List<Integer> mins = new ArrayList<>(); for (String t : timePoints) { String[] time = t.split(":"); mins.add(Integer.parseInt(time[0]) * 60 + Integer.parseInt(time[1])); } Collections.sort(mins); mins.add(mins.get(0) + 24 * 60); int res = 24 * 60; for (int i = 1; i < mins.size(); ++i) { res = Math.min(res, mins.get(i) - mins.get(i - 1)); } return res; } }
-
class Solution { public: int findMinDifference(vector<string>& timePoints) { if (timePoints.size() > 24 * 60) return 0; vector<int> mins; for (auto t : timePoints) mins.push_back(stoi(t.substr(0, 2)) * 60 + stoi(t.substr(3))); sort(mins.begin(), mins.end()); mins.push_back(mins[0] + 24 * 60); int res = 24 * 60; for (int i = 1; i < mins.size(); ++i) res = min(res, mins[i] - mins[i - 1]); return res; } };
-
''' >>> a = "000" >>> int(a) 0 >>> a = "00011" >>> int(a) 11 ''' class Solution: def findMinDifference(self, timePoints: List[str]) -> int: if len(timePoints) > 24 * 60: return 0 mins = sorted(int(t[:2]) * 60 + int(t[3:]) for t in timePoints) mins.append(mins[0] + 24 * 60) # make it a circle, linking 1st and last slot return min(abs(a - b) for a, b in pairwise(mins)) # below also works, same logic # res = mins[-1] # for i in range(1, len(mins)): # res = min(res, mins[i] - mins[i - 1]) # return res ############ class Solution(object): def findMinDifference(self, timePoints): """ :type timePoints: List[str] :rtype: int """ ans = 24 * 60 times = [0] * len(timePoints) for i, time in enumerate(timePoints): h, m = map(int, time.split(":")) times[i] = h * 60 + m times.sort() for i in range(len(times) - 1): ans = min(ans, abs(times[i] - times[i + 1])) return min(ans, 1440 - abs(times[0] - times[-1]))
-
func findMinDifference(timePoints []string) int { if len(timePoints) > 24*60 { return 0 } var mins []int for _, t := range timePoints { time := strings.Split(t, ":") h, _ := strconv.Atoi(time[0]) m, _ := strconv.Atoi(time[1]) mins = append(mins, h*60+m) } sort.Ints(mins) mins = append(mins, mins[0]+24*60) res := 24 * 60 for i := 1; i < len(mins); i++ { res = min(res, mins[i]-mins[i-1]) } return res }
-
function findMinDifference(timePoints: string[]): number { const mins = timePoints .map(item => Number(item.slice(0, 2)) * 60 + Number(item.slice(3, 5))) .sort((a, b) => a - b); const n = mins.length; let res = Infinity; for (let i = 0; i < n - 1; i++) { res = Math.min(res, mins[i + 1] - mins[i]); } const first = mins[0] + 24 * 60; const last = mins[n - 1]; res = Math.min(res, first - last); return res; }
-
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 } }