Created
March 11, 2019 19:54
-
-
Save janosgyerik/8f1f51d5e64e10f79ef5cce7705b5e7c to your computer and use it in GitHub Desktop.
Revisions
-
janosgyerik renamed this gist
Mar 11, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
janosgyerik created this gist
Mar 11, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ // 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; } }