Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
732. My Calendar III
// solution to: 732. My Calendar III https://leetcode.com/problems/my-calendar-iii/
class MyCalendarThree {
private final Comparator<Pos> comparator = Comparator.<Pos>comparingInt(p -> p.pos)
.thenComparing((p1, p2) -> {
if (p1.mark == p2.mark) return Integer.compare(p1.hashCode(), p2.hashCode());
if (p1.mark == Mark.END) return -1;
return 1;
});
private final SortedSet<Pos> marks = new TreeSet<>(comparator);
static class Pos {
final int pos;
final Mark mark;
Pos(int pos, Mark mark) {
this.pos = pos;
this.mark = mark;
}
}
enum Mark {
START,
END
}
public int book(int start, int end) {
marks.add(new Pos(start, Mark.START));
marks.add(new Pos(end, Mark.END));
int count = 0;
int max = 0;
for (Pos pos : marks) {
if (pos.mark == Mark.START) {
count++;
max = Math.max(max, count);
} else {
count--;
}
}
return max;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.