Skip to content

Commit 2c45527

Browse files
solves #2446: Determine if Two Events Have Conflict in java
1 parent a35c8dc commit 2c45527

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@
774774
| 2432 | [The Employee That Worked on the Longest Task](https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task) | [![Java](assets/java.png)](src/TheEmployeeThatWorkedOnTheLongestTask.java) | |
775775
| 2437 | [Number of Valid Clock Times](https://leetcode.com/problems/number-of-valid-clock-times) | [![Java](assets/java.png)](src/NumberOfValidClockTimes.java) | |
776776
| 2441 | [Largest Positive Integer That Exists With Its Negative](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative) | [![Java](assets/java.png)](src/LargestPositiveIntegerThatExistsWithItsNegative.java) | |
777-
| 2446 | [Determine if Two Events Have Conflict](https://leetcode.com/problems/determine-if-two-events-have-conflict) | | |
777+
| 2446 | [Determine if Two Events Have Conflict](https://leetcode.com/problems/determine-if-two-events-have-conflict) | [![Java](assets/java.png)](src/DetermineIfTwoEventsHaveConflict.java) | |
778778
| 2451 | [Odd String Difference](https://leetcode.com/problems/odd-string-difference) | | |
779779
| 2455 | [Average Value of Even Numbers That Are Divisible by Three](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three) | | |
780780
| 2460 | [Apply Operations to an Array](https://leetcode.com/problems/apply-operations-to-an-array) | | |
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// https://leetcode.com/problems/determine-if-two-events-have-conflict
2+
// T: O(1)
3+
// S: O(1)
4+
5+
public class DetermineIfTwoEventsHaveConflict {
6+
public boolean haveConflict(String[] event1, String[] event2) {
7+
final int[] event1EpochMinutes = getEpochMinutes(event1);
8+
final int[] event2EpochMinutes = getEpochMinutes(event2);
9+
return isIntersecting(event1EpochMinutes, event2EpochMinutes);
10+
}
11+
12+
private boolean isIntersecting(int[] event1, int[] event2) {
13+
return Math.min(event1[1], event2[1]) >= Math.max(event1[0], event2[0]);
14+
}
15+
16+
private int[] getEpochMinutes(String[] event) {
17+
return new int[] { getEpochMinutes(event[0]), getEpochMinutes(event[1])};
18+
}
19+
20+
private int getEpochMinutes(String event) {
21+
final String hh = event.substring(0, 2), mm = event.substring(3);
22+
return toInt(hh) * 60 + toInt(mm);
23+
}
24+
25+
private int toInt(String string) {
26+
int result = 0;
27+
for (int index = 0 ; index < string.length() ; index++) {
28+
result = 10 * result + (string.charAt(index) - '0');
29+
}
30+
return result;
31+
}
32+
}

0 commit comments

Comments
 (0)