public static class Event {
public int start, finish;
public Event(int start, int finish) {
this.start = start;
this.finish = finish;
}
@Override
public String toString() {
return "[" + start + "," + finish + "]";
}
}
private static class Endpoint implements Comparable<Endpoint> {
public int time;
public boolean isStart;
public int compareTo(Endpoint e) {
if (time != e.time) {
return Integer.compare(time, e.time);
}
return isStart && !e.isStart ? -1 : !isStart && e.isStart ? 1 : 0;
}
Endpoint(int t, boolean is) {
time = t;
isStart = is;
}
}
public static int findMaxSimultaneousEvents(List<Event> A) {
List<Endpoint> E = new ArrayList<>();
for (Event event : A) {
E.add(new Endpoint(event.start, true));
E.add(new Endpoint(event.finish, false));
}
Collections.sort(E);
int maxNumSimultaneousEvents = 0, numSimultaneousEvents = 0;
for (Endpoint endpoint : E) {
if (endpoint.isStart) {
++numSimultaneousEvents;
maxNumSimultaneousEvents
= Math.max(numSimultaneousEvents, maxNumSimultaneousEvents);
} else {
--numSimultaneousEvents;
}
}
return maxNumSimultaneousEvents;
}