-
Notifications
You must be signed in to change notification settings - Fork 16
/
DetermineIfTwoEventsHaveConflict.java
32 lines (27 loc) · 1.09 KB
/
DetermineIfTwoEventsHaveConflict.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// https://leetcode.com/problems/determine-if-two-events-have-conflict
// T: O(1)
// S: O(1)
public class DetermineIfTwoEventsHaveConflict {
public boolean haveConflict(String[] event1, String[] event2) {
final int[] event1EpochMinutes = getEpochMinutes(event1);
final int[] event2EpochMinutes = getEpochMinutes(event2);
return isIntersecting(event1EpochMinutes, event2EpochMinutes);
}
private boolean isIntersecting(int[] event1, int[] event2) {
return Math.min(event1[1], event2[1]) >= Math.max(event1[0], event2[0]);
}
private int[] getEpochMinutes(String[] event) {
return new int[] { getEpochMinutes(event[0]), getEpochMinutes(event[1])};
}
private int getEpochMinutes(String event) {
final String hh = event.substring(0, 2), mm = event.substring(3);
return toInt(hh) * 60 + toInt(mm);
}
private int toInt(String string) {
int result = 0;
for (int index = 0 ; index < string.length() ; index++) {
result = 10 * result + (string.charAt(index) - '0');
}
return result;
}
}