Skip to content

Instantly share code, notes, and snippets.

@janosgyerik
Created March 11, 2019 19:54
Show Gist options
  • Save janosgyerik/8f1f51d5e64e10f79ef5cce7705b5e7c to your computer and use it in GitHub Desktop.
Save janosgyerik/8f1f51d5e64e10f79ef5cce7705b5e7c to your computer and use it in GitHub Desktop.

Revisions

  1. janosgyerik renamed this gist Mar 11, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. janosgyerik created this gist Mar 11, 2019.
    43 changes: 43 additions & 0 deletions 732-my-calendar-3.md
    Original 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;
    }
    }