Skip to content

Commit 94e75bf

Browse files
committed
ExamRoom855
1 parent 07a5817 commit 94e75bf

File tree

2 files changed

+137
-2
lines changed

2 files changed

+137
-2
lines changed

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@
5353
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
5454

5555

56-
# Total: 476
56+
# Total: 477
5757

5858
| Easy | Medium | Hard | - |
5959
|:-------:|:-------:|:----:|:-:|
60-
| 127 | 266 | 77 | 6 |
60+
| 127 | 267 | 77 | 6 |
6161

6262

6363
| Question | Solution | Difficulty |
@@ -518,6 +518,7 @@
518518
| [845. Longest Mountain in Array](https://leetcode.com/problems/longest-mountain-in-array/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/LongestMountainInArray845.java) | Medium |
519519
| [846. Hand of Straights](https://leetcode.com/problems/hand-of-straights/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/HandOfStraights846.java) | Medium |
520520
| [849. Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MaximizeDistanceToClosestPerson849.java) | Easy |
521+
| [855. Exam Room](https://leetcode.com/problems/exam-room/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ExamRoom855.java) | Medium |
521522
| [859. Buddy Strings](https://leetcode.com/problems/buddy-strings/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/BuddyStrings859.java) | Easy |
522523
| [860. Lemonade Change](https://leetcode.com/problems/lemonade-change/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/LemonadeChange860.java) | Easy |
523524
| [862. Shortest Subarray with Sum at Least K](https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ShortestSubarrayWithSumAtLeastK862.java) | Hard |

Diff for: src/ExamRoom855.java

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/**
2+
* In an exam room, there are N seats in a single row, numbered 0, 1, 2, ..., N-1.
3+
*
4+
* When a student enters the room, they must sit in the seat that maximizes the
5+
* distance to the closest person. If there are multiple such seats, they sit
6+
* in the seat with the lowest number. (Also, if no one is in the room, then
7+
* the student sits at seat number 0.)
8+
*
9+
* Return a class ExamRoom(int N) that exposes two functions: ExamRoom.seat()
10+
* returning an int representing what seat the student sat in, and
11+
* ExamRoom.leave(int p) representing that the student in seat number p now
12+
* leaves the room. It is guaranteed that any calls to ExamRoom.leave(p) have
13+
* a student sitting in seat p.
14+
*
15+
* Example 1:
16+
* Input: ["ExamRoom","seat","seat","seat","seat","leave","seat"], [[10],[],[],[],[],[4],[]]
17+
* Output: [null,0,9,4,2,null,5]
18+
*
19+
* Explanation:
20+
* ExamRoom(10) -> null
21+
* seat() -> 0, no one is in the room, then the student sits at seat number 0.
22+
* seat() -> 9, the student sits at the last seat number 9.
23+
* seat() -> 4, the student sits at the last seat number 4.
24+
* seat() -> 2, the student sits at the last seat number 2.
25+
* leave(4) -> null
26+
* seat() -> 5, the student​​​​​​​ sits at the last seat number 5.
27+
​​​​​​​ *
28+
* Note:
29+
* 1 <= N <= 10^9
30+
* ExamRoom.seat() and ExamRoom.leave() will be called at most 10^4 times across all test cases.
31+
* Calls to ExamRoom.leave(p) are guaranteed to have a student currently sitting in seat number p.
32+
*/
33+
34+
35+
public class ExamRoom855 {
36+
class ExamRoom {
37+
private PriorityQueue<Range> pq;
38+
private Map<Integer, Range> lefts;
39+
private Map<Integer, Range> rights;
40+
private int N;
41+
42+
public ExamRoom(int N) {
43+
this.pq = new PriorityQueue(1, new Comparator<Range>() {
44+
@Override
45+
public int compare(Range r1, Range r2) {
46+
int rangeDiff = Integer.compare(dis(r2), dis(r1));
47+
if (rangeDiff != 0) return rangeDiff;
48+
return Integer.compare(r1.left, r2.left);
49+
}
50+
});
51+
this.lefts = new HashMap<>();
52+
this.rights = new HashMap<>();
53+
this.N = N;
54+
55+
Range first = new Range(-1, N);
56+
this.pq.add(first);
57+
this.lefts.put(first.left, first);
58+
this.rights.put(first.right, first);
59+
}
60+
61+
public int seat() {
62+
if (this.pq.isEmpty()) return -1;
63+
Range curr = this.pq.poll();
64+
this.lefts.remove(curr.left);
65+
this.rights.remove(curr.right);
66+
67+
int pos = sitPos(curr);
68+
Range l = new Range(curr.left, pos);
69+
this.lefts.put(l.left, l);
70+
this.rights.put(l.right, l);
71+
if (curr.left + 1 < pos) {
72+
this.pq.add(l);
73+
}
74+
75+
Range r = new Range(pos, curr.right);
76+
this.lefts.put(r.left, r);
77+
this.rights.put(r.right, r);
78+
if (pos + 1 < curr.right) {
79+
this.pq.add(r);
80+
}
81+
return pos;
82+
}
83+
84+
public void leave(int p) {
85+
Range r = this.lefts.get(p);
86+
Range l = this.rights.get(p);
87+
this.pq.remove(r);
88+
this.pq.remove(l);
89+
90+
Range merged = new Range(l.left, r.right);
91+
this.pq.add(merged);
92+
this.lefts.put(merged.left, merged);
93+
this.rights.put(merged.right, merged);
94+
}
95+
96+
private int sitPos(Range r) {
97+
return sitPos(r.left, r.right);
98+
}
99+
100+
private int sitPos(int left, int right) {
101+
if (left + 1 >= right) return -1;
102+
if (left < 0) return 0;
103+
if (right >= this.N) return this.N - 1;
104+
return (right - left) / 2 + left;
105+
}
106+
107+
private int dis(Range r) {
108+
return dis(r.left, r.right);
109+
}
110+
111+
private int dis(int left, int right) {
112+
if (left < 0) return right;
113+
if (right >= this.N) return this.N - 1 - left;
114+
return (right - left) / 2;
115+
}
116+
}
117+
118+
class Range {
119+
int left;
120+
int right;
121+
Range (int l, int r) {
122+
this.left = l;
123+
this.right = r;
124+
}
125+
}
126+
127+
/**
128+
* Your ExamRoom object will be instantiated and called as such:
129+
* ExamRoom obj = new ExamRoom(N);
130+
* int param_1 = obj.seat();
131+
* obj.leave(p);
132+
*/
133+
134+
}

0 commit comments

Comments
 (0)