Skip to content

Commit 148a0b9

Browse files
committed
add 855 java, progress
1 parent a238ee1 commit 148a0b9

File tree

6 files changed

+345
-9
lines changed

6 files changed

+345
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@
527527
373 | [Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/) | [Python](./leetcode_python/Heap/find-k-pairs-with-smallest-sums.py) | _O(k * log(min(n, m, k)))_ | _O(min(n, m, k))_ | Medium || AGAIN (not start)
528528
378 | [Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/) | [Python](./leetcode_python/Heap/kth-smallest-element-in-a-sorted-matrix.py) | _O(k * log(min(n, m, k)))_ | _O(min(n, m, k))_ | Medium | LintCode | AGAIN (not start)
529529
846 | [Hand of Straights](https://leetcode.com/problems/hand-of-straights/) | [Python](./leetcode_python/Heap/hand-of-straights.py) | _O(nlogn)_ | _O(n)_ | Medium |LC 1296, google, hashmap| OK*
530-
855 | [Exam Room](https://leetcode.com/problems/exam-room/) | [Python](./leetcode_python/Heap/exam-room.py) | seat: _O(logn)_ <br> leave: _O(logn)_ | _O(n)_ | Medium | BST, Hash, `trick` | AGAIN* (2) (not start)
530+
855 | [Exam Room](https://leetcode.com/problems/exam-room/) | [Python](./leetcode_python/Heap/exam-room.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Heap/ExamRoom.java) | seat: _O(logn)_ <br> leave: _O(logn)_ | _O(n)_ | Medium | treeSet, BST, Hash, `trick` | AGAIN* (3) (not start)
531531
295 | [Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) | [Python](./leetcode_python/Heap/find_median_from_data_stream.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Heap/FindMedianFromDataStream.java) | | | Hard |Curated Top 75, priority queue, trick ,heap, stream, `amazon`| AGAIN****** (6)
532532
703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/) | [Python](./leetcode_python/Heap/kth_largest_element_in_a_stream.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Heap/KthLargestElementInAStream.java) | | | Easy |heap, priority queue, `amazon`| AGAIN***** (4)
533533
1046| [Last Stone Weight](https://leetcode.com/problems/last-stone-weight/) | [Python](./leetcode_python/Heap/last-stone-weight.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Heap/LastStoneWeight.java)||| Easy |Heap, Priority Queue| OK

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-10-27
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-10-26
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241027: 855
12
20241026: 932
23
20241024: 951,792
34
20241020: 163,1048

data/to_review.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2024-12-21 -> ['855']
12
2024-12-20 -> ['932']
23
2024-12-18 -> ['951,792']
34
2024-12-14 -> ['163,1048']
@@ -8,7 +9,7 @@
89
2024-12-06 -> ['353']
910
2024-12-05 -> ['528,334']
1011
2024-12-03 -> ['1145']
11-
2024-11-30 -> ['1145,1219']
12+
2024-11-30 -> ['855', '1145,1219']
1213
2024-11-29 -> ['932']
1314
2024-11-27 -> ['951,792', '524,221,889']
1415
2024-11-26 -> ['743,889']
@@ -18,25 +19,25 @@
1819
2024-11-21 -> ['1146']
1920
2024-11-20 -> ['939']
2021
2024-11-18 -> ['430']
21-
2024-11-17 -> ['737', '363']
22+
2024-11-17 -> ['855', '737', '363']
2223
2024-11-16 -> ['932', '686,734,737', '1032,844,1011']
2324
2024-11-15 -> ['353', '947']
2425
2024-11-14 -> ['951,792', '528,334']
2526
2024-11-12 -> ['1145', '753']
2627
2024-11-11 -> ['727']
2728
2024-11-10 -> ['163,1048']
28-
2024-11-09 -> ['298,729', '1145,1219']
29+
2024-11-09 -> ['855', '298,729', '1145,1219']
2930
2024-11-08 -> ['932', '1146']
3031
2024-11-06 -> ['951,792', '524,221,889']
3132
2024-11-05 -> ['743,889']
32-
2024-11-04 -> ['737', '837', '659']
33+
2024-11-04 -> ['855', '737', '837', '659']
3334
2024-11-03 -> ['932', '686,734,737', '801,552']
3435
2024-11-02 -> ['163,1048', '353', '981', '1057,1066,1110']
35-
2024-11-01 -> ['951,792', '298,729', '528,334', '1087']
36+
2024-11-01 -> ['855', '951,792', '298,729', '528,334', '1087']
3637
2024-10-31 -> ['932', '1146']
37-
2024-10-30 -> ['1145', '939']
38-
2024-10-29 -> ['932', '951,792']
39-
2024-10-28 -> ['932', '163,1048', '430']
38+
2024-10-30 -> ['855', '1145', '939']
39+
2024-10-29 -> ['855', '932', '951,792']
40+
2024-10-28 -> ['855', '932', '163,1048', '430']
4041
2024-10-27 -> ['932', '951,792', '298,729', '737', '1145,1219', '363']
4142
2024-10-26 -> ['951,792', '1146', '686,734,737', '1032,844,1011']
4243
2024-10-25 -> ['951,792', '163,1048', '353', '947', '1110, 1055']
Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
package LeetCodeJava.Heap;
2+
3+
// https://leetcode.com/problems/exam-room/description/
4+
/**
5+
* 855. Exam Room
6+
* Medium
7+
* Topics
8+
* Companies
9+
* There is an exam room with n seats in a single row labeled from 0 to n - 1.
10+
* <p>
11+
* 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. If no one is in the room, then the student sits at seat number 0.
12+
* <p>
13+
* Design a class that simulates the mentioned exam room.
14+
* <p>
15+
* Implement the ExamRoom class:
16+
* <p>
17+
* ExamRoom(int n) Initializes the object of the exam room with the number of the seats n.
18+
* int seat() Returns the label of the seat at which the next student will set.
19+
* void leave(int p) Indicates that the student sitting at seat p will leave the room. It is guaranteed that there will be a student sitting at seat p.
20+
* <p>
21+
* <p>
22+
* Example 1:
23+
* <p>
24+
* Input
25+
* ["ExamRoom", "seat", "seat", "seat", "seat", "leave", "seat"]
26+
* [[10], [], [], [], [], [4], []]
27+
* Output
28+
* [null, 0, 9, 4, 2, null, 5]
29+
* <p>
30+
* Explanation
31+
* ExamRoom examRoom = new ExamRoom(10);
32+
* examRoom.seat(); // return 0, no one is in the room, then the student sits at seat number 0.
33+
* examRoom.seat(); // return 9, the student sits at the last seat number 9.
34+
* examRoom.seat(); // return 4, the student sits at the last seat number 4.
35+
* examRoom.seat(); // return 2, the student sits at the last seat number 2.
36+
* examRoom.leave(4);
37+
* examRoom.seat(); // return 5, the student sits at the last seat number 5.
38+
* <p>
39+
* <p>
40+
* <p>
41+
* Constraints:
42+
* <p>
43+
* 1 <= n <= 109
44+
* It is guaranteed that there is a student sitting at seat p.
45+
* At most 104 calls will be made to seat and leave.
46+
*/
47+
48+
import java.util.TreeSet;
49+
50+
/**
51+
* Your ExamRoom object will be instantiated and called as such:
52+
* ExamRoom obj = new ExamRoom(n);
53+
* int param_1 = obj.seat();
54+
* obj.leave(p);
55+
*/
56+
public class ExamRoom {
57+
58+
// V0
59+
// class ExamRoom {
60+
//
61+
// public ExamRoom(int n) {
62+
//
63+
// }
64+
//
65+
// public int seat() {
66+
//
67+
// }
68+
//
69+
// public void leave(int p) {
70+
//
71+
// }
72+
// }
73+
74+
// V1
75+
76+
// V2
77+
// (offered by gpt)
78+
public class ExamRoom_2 {
79+
private TreeSet<Integer> seats;
80+
private int n;
81+
82+
public ExamRoom_2(int n) {
83+
this.n = n;
84+
this.seats = new TreeSet<>();
85+
}
86+
87+
public int seat() {
88+
int seat = 0;
89+
90+
if (seats.size() > 0) {
91+
// Initialize the max distance and seat to sit at
92+
int maxDist = seats.first();
93+
seat = 0; // sitting at the first seat
94+
95+
Integer prev = null;
96+
for (Integer s : seats) {
97+
if (prev != null) {
98+
int dist = (s - prev) / 2;
99+
if (dist > maxDist) {
100+
maxDist = dist;
101+
seat = prev + dist;
102+
}
103+
}
104+
prev = s;
105+
}
106+
107+
// Consider sitting at the last seat
108+
if (n - 1 - seats.last() > maxDist) {
109+
seat = n - 1;
110+
}
111+
}
112+
113+
// Add the seat to the occupied set
114+
seats.add(seat);
115+
return seat;
116+
}
117+
118+
public void leave(int p) {
119+
seats.remove(p);
120+
}
121+
122+
}
123+
124+
// V3
125+
// https://leetcode.com/problems/exam-room/solutions/1331530/easy-to-understand-java-solution-with-explanation-treeset/0
126+
class ExamRoom_3 {
127+
128+
int capacity; // this is just to keep track of the max allowed size
129+
TreeSet<Integer> seats;
130+
/* why treeset, because seats will be sorted automatically
131+
and get first()/last() element in log(n) time.
132+
*/
133+
134+
public ExamRoom_3(int n) {
135+
this.capacity = n;
136+
this.seats = new TreeSet<>();
137+
}
138+
139+
public int seat() {
140+
int seatNumber = 0;
141+
/*
142+
Return 0 for first attempt ( as mentioned in question)
143+
Otherwise, we need to calculate the max distance by checking the whole treeset : O(n) time.
144+
Note that "distance" variable is initialized to first appearing seat,
145+
why because the distance calculation is based on current seat and the seat before that.
146+
Find the maximum distance and update the seat number accordingly.
147+
distance calculation -> (current seat - previous seat )/ 2
148+
Update the max distance at each step.
149+
New seat number will be -> previous seat number + max distance
150+
151+
Now, before returning the end result, check for one more edge case:
152+
That is, if the max distance calculated is less than -> capacity-1-seats.last()
153+
154+
Why because -> if last seat number in treeset is far from last position,
155+
then the largest distance possible is the last position.
156+
157+
*/
158+
if (seats.size() > 0) {
159+
Integer prev = null;
160+
int distance = seats.first();
161+
for (Integer seat : seats) {
162+
if (prev != null) {
163+
int d = (seat - prev) / 2;
164+
if (distance < d) {
165+
distance = d;
166+
seatNumber = prev + distance;
167+
}
168+
}
169+
prev = seat;
170+
}
171+
172+
if (distance < capacity - 1 - seats.last()) {
173+
seatNumber = capacity - 1;
174+
}
175+
}
176+
seats.add(seatNumber);
177+
return seatNumber;
178+
}
179+
180+
public void leave(int p) {
181+
seats.remove(p);
182+
/* simply remove the seat number from treeset
183+
and treeset will be automatically adjust its order in log(n) time.
184+
*/
185+
}
186+
}
187+
188+
// V4
189+
// https://leetcode.com/problems/exam-room/solutions/2824662/java-simple-one-treeset-solution-with-o-n-seat-and-o-logn-leave/
190+
class ExamRoom_4 {
191+
int N;
192+
TreeSet<Integer> seats;
193+
public ExamRoom_4(int n) {
194+
seats = new TreeSet<>(); // ordered set to keep track of students
195+
this.N = n;
196+
}
197+
198+
public int seat() {
199+
int student = 0;
200+
201+
if (seats.size() > 0) { // if at least one student, otherwise seat at 0
202+
int dist = seats.first(); // the student at lowest seat
203+
Integer prev = null;
204+
for (Integer seat: seats) {
205+
if (prev != null) {
206+
int d = (seat - prev)/2; // every time we see a new student we can seat them between 2 other students
207+
if (d > dist) { // select the max range
208+
dist = d;
209+
student = prev + d;
210+
}
211+
}
212+
prev = seat;
213+
}
214+
if (N - 1 - seats.last() > dist) {
215+
student = N-1;
216+
}
217+
}
218+
seats.add(student);
219+
return student;
220+
}
221+
222+
public void leave(int p) {
223+
seats.remove(p);
224+
}
225+
}
226+
227+
// V5
228+
// https://leetcode.com/problems/exam-room/solutions/2085063/java-o-logn-seat-leave-treeset-solution/
229+
class ExamRoom_5 {
230+
231+
private final int max;
232+
private final TreeSet<Interval> available;
233+
private final TreeSet<Integer> taken;
234+
235+
public ExamRoom_5(int n) {
236+
this.max = n - 1;
237+
this.available = new TreeSet<>((a, b) -> {
238+
int distA = getMinDistance(a);
239+
int distB = getMinDistance(b);
240+
return distA == distB ? a.s - b.s : distB - distA;
241+
});
242+
this.available.add(new Interval(0, max));
243+
this.taken = new TreeSet<>();
244+
}
245+
246+
public int seat() {
247+
Interval inter = available.pollFirst();
248+
int idx = getInsertPosition(inter);
249+
taken.add(idx);
250+
if ((idx - 1) - inter.s >= 0)
251+
available.add(new Interval(inter.s, idx - 1));
252+
if (inter.e - (idx + 1) >= 0)
253+
available.add(new Interval(idx + 1, inter.e));
254+
return idx;
255+
}
256+
257+
public void leave(int p) {
258+
taken.remove(p);
259+
Integer lo = taken.lower(p);
260+
if (lo == null)
261+
lo = -1;
262+
Integer hi = taken.higher(p);
263+
if (hi == null)
264+
hi = max + 1;
265+
available.remove(new Interval(lo + 1, p - 1));
266+
available.remove(new Interval(p + 1, hi - 1));
267+
available.add(new Interval(lo + 1, hi - 1));
268+
}
269+
270+
private int getInsertPosition(Interval inter) {
271+
if (inter.s == 0)
272+
return 0;
273+
else if (inter.e == max)
274+
return max;
275+
else
276+
return inter.s + (inter.e - inter.s) / 2;
277+
}
278+
279+
private int getMinDistance(Interval in) {
280+
return in.s == 0 || in.e == max ? in.e - in.s : (in.e - in.s) / 2;
281+
}
282+
283+
private final class Interval {
284+
private final int s;
285+
private final int e;
286+
287+
Interval(int s, int e) {
288+
this.s = s;
289+
this.e = e;
290+
}
291+
292+
@Override
293+
public String toString() {
294+
return "[" + s + "," + e + "]";
295+
}
296+
}
297+
}
298+
299+
}

0 commit comments

Comments
 (0)