🧠
Bucket Sort Optimization (Using Boolean Array for Times)
💡 Since times are limited to 1440 minutes in a day, we can use a boolean array to mark times and find minimum difference in O(n) time, avoiding sorting overhead.
Intuition
Mark each time point in a boolean array of size 1440, then scan the array to find the minimum difference between consecutive true entries, including circular wrap.
Algorithm
- Create a boolean array of size 1440 initialized to false.
- Convert each time to minutes and mark the corresponding index true; if duplicate found, return 0 immediately.
- Scan the boolean array to find the minimum difference between consecutive true indices.
- Also consider the circular difference between the last and first true indices.
- Return the minimum difference found.
💡 This approach uses extra space but achieves linear time by exploiting the fixed time range and handles duplicates efficiently.
def findMinDifference(timePoints):
seen = [False] * 1440
for t in timePoints:
h, m = map(int, t.split(':'))
total = h * 60 + m
if seen[total]:
return 0 # duplicate times mean min diff is 0
seen[total] = True
prev = -1
first = -1
min_diff = 1440
for i in range(1440):
if seen[i]:
if prev != -1:
min_diff = min(min_diff, i - prev)
else:
first = i
prev = i
min_diff = min(min_diff, 1440 - prev + first)
return min_diff
# Example usage
if __name__ == '__main__':
print(findMinDifference(["23:59", "00:00"])) # Output: 1
Line Notes
seen = [False] * 1440Create fixed-size array to mark times
if seen[total]: return 0If duplicate time found, minimum difference is zero
for i in range(1440)Scan all possible times in order
if seen[i]Check if this time was marked
min_diff = min(min_diff, i - prev)Update min difference between consecutive marked times
min_diff = min(min_diff, 1440 - prev + first)Check circular difference between last and first marked times
return min_diffReturn the smallest difference
import java.util.*;
public class Solution {
public static int findMinDifference(List<String> timePoints) {
boolean[] seen = new boolean[1440];
for (String t : timePoints) {
String[] parts = t.split(":");
int h = Integer.parseInt(parts[0]);
int m = Integer.parseInt(parts[1]);
int total = h * 60 + m;
if (seen[total]) return 0;
seen[total] = true;
}
int prev = -1, first = -1, minDiff = 1440;
for (int i = 0; i < 1440; i++) {
if (seen[i]) {
if (prev != -1) {
minDiff = Math.min(minDiff, i - prev);
} else {
first = i;
}
prev = i;
}
}
minDiff = Math.min(minDiff, 1440 - prev + first);
return minDiff;
}
public static void main(String[] args) {
List<String> times = Arrays.asList("23:59", "00:00");
System.out.println(findMinDifference(times)); // Output: 1
}
}
Line Notes
boolean[] seen = new boolean[1440]Create fixed-size boolean array for times
if (seen[total]) return 0Return 0 immediately if duplicate time found
for (int i = 0; i < 1440; i++)Scan all possible times in order
if (seen[i])Check if time is marked
minDiff = Math.min(minDiff, i - prev)Update min difference between consecutive marked times
minDiff = Math.min(minDiff, 1440 - prev + first)Check circular difference between last and first marked times
return minDiffReturn the smallest difference
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int findMinDifference(vector<string>& timePoints) {
vector<bool> seen(1440, false);
for (auto& t : timePoints) {
int h = stoi(t.substr(0, 2));
int m = stoi(t.substr(3, 2));
int total = h * 60 + m;
if (seen[total]) return 0;
seen[total] = true;
}
int prev = -1, first = -1, minDiff = 1440;
for (int i = 0; i < 1440; i++) {
if (seen[i]) {
if (prev != -1) {
minDiff = min(minDiff, i - prev);
} else {
first = i;
}
prev = i;
}
}
minDiff = min(minDiff, 1440 - prev + first);
return minDiff;
}
int main() {
vector<string> times = {"23:59", "00:00"};
cout << findMinDifference(times) << endl; // Output: 1
return 0;
}
Line Notes
vector<bool> seen(1440, false)Create fixed-size boolean vector for times
if (seen[total]) return 0Return 0 if duplicate time found
for (int i = 0; i < 1440; i++)Scan all possible times in order
if (seen[i])Check if time is marked
minDiff = min(minDiff, i - prev)Update min difference between consecutive marked times
minDiff = min(minDiff, 1440 - prev + first)Check circular difference between last and first marked times
return minDiffReturn the smallest difference
function findMinDifference(timePoints) {
const seen = new Array(1440).fill(false);
for (const t of timePoints) {
const [h, m] = t.split(':').map(Number);
const total = h * 60 + m;
if (seen[total]) return 0;
seen[total] = true;
}
let prev = -1, first = -1, minDiff = 1440;
for (let i = 0; i < 1440; i++) {
if (seen[i]) {
if (prev !== -1) {
minDiff = Math.min(minDiff, i - prev);
} else {
first = i;
}
prev = i;
}
}
minDiff = Math.min(minDiff, 1440 - prev + first);
return minDiff;
}
// Example usage
console.log(findMinDifference(["23:59", "00:00"])); // Output: 1
Line Notes
const seen = new Array(1440).fill(false)Create fixed-size boolean array for times
if (seen[total]) return 0Return 0 if duplicate time found
for (let i = 0; i < 1440; i++)Scan all possible times in order
if (seen[i])Check if time is marked
minDiff = Math.min(minDiff, i - prev)Update min difference between consecutive marked times
minDiff = Math.min(minDiff, 1440 - prev + first)Check circular difference between last and first marked times
return minDiffReturn the smallest difference
TimeO(n)
SpaceO(1) (fixed size 1440 array)
Marking times is O(n), scanning fixed 1440 slots is O(1) constant time. Space is fixed at 1440 booleans regardless of input size.
💡 This approach is fastest for large inputs because it avoids sorting and uses constant space relative to input size.
Interview Verdict: Accepted / Most optimal for large inputs
This approach leverages problem constraints for optimal performance and is a great example of using domain knowledge.