Monday, January 14, 2019

855. Exam Room

855Exam Room
In an exam room, there are N seats in a single row, numbered 0, 1, 2, ..., N-1.
When a student enters the room, they must sit in the seat that maximizes the distance to the closest person.  If there are multiple such seats, they sit in the seat with the lowest number.  (Also, if no one is in the room, then the student sits at seat number 0.)
Return a class ExamRoom(int N) that exposes two functions: ExamRoom.seat() returning an int representing what seat the student sat in, and ExamRoom.leave(int p) representing that the student in seat number p now leaves the room.  It is guaranteed that any calls to ExamRoom.leave(p) have a student sitting in seat p.

Example 1:
Input: ["ExamRoom","seat","seat","seat","seat","leave","seat"], [[10],[],[],[],[],[4],[]]
Output: [null,0,9,4,2,null,5]
Explanation:
ExamRoom(10) -> null
seat() -> 0, no one is in the room, then the student sits at seat number 0.
seat() -> 9, the student sits at the last seat number 9.
seat() -> 4, the student sits at the last seat number 4.
seat() -> 2, the student sits at the last seat number 2.
leave(4) -> null
seat() -> 5, the student sits at the last seat number 5.
Note:
  1. 1 <= N <= 10^9
  2. ExamRoom.seat() and ExamRoom.leave() will be called at most 10^4 times across all test cases.
  3. Calls to ExamRoom.leave(p) are guaranteed to have a student currently sitting in seat number p.
------------------------
Solution #1 ref: https://leetcode.com/problems/exam-room/solution/
用TreeSet来存位置,seat O(n), leave O(logn)
注意要处理起始点跟终止点
class ExamRoom {
    private TreeSet<Integer> set;
    private int n;
    public ExamRoom(int N) {
        set = new TreeSet<>();
        n = N;
    }
    
    public int seat() {
        int st = 0;
        if (set.size() > 0) {
            
            int max = set.first();
            Integer pre = null;
            
            for (Integer cur : set) {
                
                if (pre != null) {
                    int dis = (cur - pre) / 2;
                    if (dis > max) {
                        max = dis;
                        st = pre + dis;
                    }
                }
                pre = cur;
            }
            
            if (n - 1 - set.last() > max) {
                st = n - 1;
            }
        }
        
        set.add(st);
        return st;
    }
    
    public void leave(int p) {
        set.remove(p);
    }
}

/**
 * Your ExamRoom object will be instantiated and called as such:
 * ExamRoom obj = new ExamRoom(N);
 * int param_1 = obj.seat();
 * obj.leave(p);
 */

Solution #2
PriorityQueue里存的是interval,按距离
要点:
1. 距离的计算方式,开头和末尾
2. interval存的是[start, mid] [mid, end], 前后2个interval会共享一个点
class ExamRoom {

    private PriorityQueue<Interval> que;
    private int n;
    public ExamRoom(int N) {
        que = new PriorityQueue<>((a, b) -> {
            if (a.dis == b.dis) return a.start - b.start;
            return b.dis - a.dis;
        });
        
        n = N;
        que.add(new Interval(-1, n));
    }
    
    public int seat() {
        Interval top = que.poll();
            
        int mid = (top.start + top.end) / 2;
        if (top.start == -1) {
            que.add(new Interval(0, top.end));
            mid = 0;
        }else if (top.end == n) {
            que.add(new Interval(top.start, n - 1));
            mid = n - 1;
        }else {
            que.add(new Interval(top.start, mid));
            que.add(new Interval(mid, top.end));
        }
        return mid;
    }
    
    public void leave(int p) {
        Interval merged = new Interval(-1,n);
        Interval head = null, tail = null;
        for (Interval top : que) {            
            if (p == 0 && top.start == 0) {
                merged.end = top.end;
                head = top;
            }else if (p == n - 1 && top.end == n - 1) {
                merged.start = top.start;
                head = top;
            }else if (top.start == p) {
                merged.end = top.end;
                head = top;
            }else if (top.end == p) {
                merged.start = top.start;
                tail = top;
            }
        }
        
        que.remove(head);
        que.remove(tail);
        que.add(new Interval(merged.start, merged.end));
    }
    
    class Interval{
        public int start;
        public int end;
        public int dis;
        public Interval(int start, int end) {
            this.start = start;
            this.end = end;
            if (start == -1) {
                dis = end;
            }else if (end == n) {
                dis = n - start - 1;
            }else {
                dis = (end - start) / 2;
            }
        }
    }
}

No comments:

Post a Comment