Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit23a8c9b

Browse files
855_ExamRoom855
1 parent7c87047 commit23a8c9b

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

‎855_ExamRoom855.java‎

Lines changed: 134 additions & 0 deletions
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+
publicclassExamRoom855 {
36+
classExamRoom {
37+
privatePriorityQueue<Range>pq;
38+
privateMap<Integer,Range>lefts;
39+
privateMap<Integer,Range>rights;
40+
privateintN;
41+
42+
publicExamRoom(intN) {
43+
this.pq =newPriorityQueue(1,newComparator<Range>() {
44+
@Override
45+
publicintcompare(Ranger1,Ranger2) {
46+
intrangeDiff =Integer.compare(dis(r2),dis(r1));
47+
if (rangeDiff !=0)returnrangeDiff;
48+
returnInteger.compare(r1.left,r2.left);
49+
}
50+
});
51+
this.lefts =newHashMap<>();
52+
this.rights =newHashMap<>();
53+
this.N =N;
54+
55+
Rangefirst =newRange(-1,N);
56+
this.pq.add(first);
57+
this.lefts.put(first.left,first);
58+
this.rights.put(first.right,first);
59+
}
60+
61+
publicintseat() {
62+
if (this.pq.isEmpty())return -1;
63+
Rangecurr =this.pq.poll();
64+
this.lefts.remove(curr.left);
65+
this.rights.remove(curr.right);
66+
67+
intpos =sitPos(curr);
68+
Rangel =newRange(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+
Ranger =newRange(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+
returnpos;
82+
}
83+
84+
publicvoidleave(intp) {
85+
Ranger =this.lefts.get(p);
86+
Rangel =this.rights.get(p);
87+
this.pq.remove(r);
88+
this.pq.remove(l);
89+
90+
Rangemerged =newRange(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+
privateintsitPos(Ranger) {
97+
returnsitPos(r.left,r.right);
98+
}
99+
100+
privateintsitPos(intleft,intright) {
101+
if (left +1 >=right)return -1;
102+
if (left <0)return0;
103+
if (right >=this.N)returnthis.N -1;
104+
return (right -left) /2 +left;
105+
}
106+
107+
privateintdis(Ranger) {
108+
returndis(r.left,r.right);
109+
}
110+
111+
privateintdis(intleft,intright) {
112+
if (left <0)returnright;
113+
if (right >=this.N)returnthis.N -1 -left;
114+
return (right -left) /2;
115+
}
116+
}
117+
118+
classRange {
119+
intleft;
120+
intright;
121+
Range (intl,intr) {
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)

[8]ページ先頭

©2009-2025 Movatter.jp