Minimum Time Difference – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
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”.
C++ Minimum Time Difference LeetCode Solution
class Solution {
public:
int findMinDifference(vector<string>& times) {
int n = times.size();
sort(times.begin(), times.end());
int mindiff = INT_MAX;
for (int i = 0; i < times.size(); i++) {
int diff = abs(timeDiff(times[(i - 1 + n) % n], times[i]));
diff = min(diff, 1440 - diff);
mindiff = min(mindiff, diff);
}
return mindiff;
}
private:
int timeDiff(string t1, string t2) {
int h1 = stoi(t1.substr(0, 2));
int m1 = stoi(t1.substr(3, 2));
int h2 = stoi(t2.substr(0, 2));
int m2 = stoi(t2.substr(3, 2));
return (h2 - h1) * 60 + (m2 - m1);
}
};
Java Minimum Time Difference LeetCode Solution
public class Solution {
public int findMinDifference(List<String> timePoints) {
boolean[] mark = new boolean[24 * 60];
for (String time : timePoints) {
String[] t = time.split(":");
int h = Integer.parseInt(t[0]);
int m = Integer.parseInt(t[1]);
if (mark[h * 60 + m]) return 0;
mark[h * 60 + m] = true;
}
int prev = 0, min = Integer.MAX_VALUE;
int first = Integer.MAX_VALUE, last = Integer.MIN_VALUE;
for (int i = 0; i < 24 * 60; i++) {
if (mark[i]) {
if (first != Integer.MAX_VALUE) {
min = Math.min(min, i - prev);
}
first = Math.min(first, i);
last = Math.max(last, i);
prev = i;
}
}
min = Math.min(min, (24 * 60 - last + first));
return min;
}
}
Python 3 Minimum Time Difference LeetCode Solution
def findMinDifference(self, A):
def convert(time):
return int(time[:2]) * 60 + int(time[3:])
minutes = map(convert, A)
minutes.sort()
return min( (y - x) % (24 * 60)
for x, y in zip(minutes, minutes[1:] + minutes[:1]) )
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1