Created
March 11, 2019 19:54
-
-
Save janosgyerik/8f1f51d5e64e10f79ef5cce7705b5e7c to your computer and use it in GitHub Desktop.
732. My Calendar III
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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