Skip to content

Commit 3e5c619

Browse files
committed
update 855 java
1 parent 148a0b9 commit 3e5c619

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
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), [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)
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`, google | 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

leetcode_java/src/main/java/LeetCodeJava/Heap/ExamRoom.java

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
*/
4747

4848
import java.util.TreeSet;
49+
import java.util.ArrayList;
50+
import java.util.Collections;
4951

5052
/**
5153
* Your ExamRoom object will be instantiated and called as such:
@@ -73,17 +75,89 @@ public class ExamRoom {
7375

7476
// V1
7577

76-
// V2
77-
// (offered by gpt)
78-
public class ExamRoom_2 {
78+
// V2-1
79+
// IDEA : Linkedlist (offered by gpt)
80+
public class ExamRoom_2_1 {
81+
82+
private int n;
83+
private ArrayList<Integer> seats;
84+
85+
public ExamRoom_2_1(int n) {
86+
this.n = n;
87+
this.seats = new ArrayList<>();
88+
}
89+
90+
public int seat() {
91+
// If no one is seated, the first student sits at seat 0
92+
if (seats.isEmpty()) {
93+
seats.add(0);
94+
return 0;
95+
}
96+
97+
// Determine where to seat the next student
98+
int maxDist = seats.get(0); // Distance from seat 0 to the first occupied seat
99+
int seat = 0; // Initially, consider the first seat
100+
101+
// Check gaps between seated students
102+
for (int i = 0; i < seats.size() - 1; i++) {
103+
// calculates the halfway distance between two students
104+
// who are already seated at positions seats.get(i) and seats.get(i + 1).
105+
int dist = (seats.get(i + 1) - seats.get(i)) / 2;
106+
if (dist > maxDist) {
107+
maxDist = dist;
108+
seat = seats.get(i) + dist; // The seat in the middle
109+
}
110+
}
111+
112+
// Check the distance from the last seated student to the last seat
113+
if (n - 1 - seats.get(seats.size() - 1) > maxDist) {
114+
seat = n - 1;
115+
}
116+
117+
// Add the student to the seat
118+
seats.add(seat);
119+
Collections.sort(seats); // Sort to maintain seat order
120+
return seat;
121+
}
122+
123+
public void leave(int p) {
124+
seats.remove(Integer.valueOf(p)); // Remove the student from the seat
125+
}
126+
127+
}
128+
129+
// V2-2
130+
// IDEA : Treeset (offered by gpt)
131+
/**
132+
* Idea:
133+
*
134+
* You can solve the problem by maintaining a sorted list of occupied seats.
135+
* Each time a student enters, you look for the largest available gap between two occupied seats,
136+
* or between an occupied seat and the room boundaries (start or end).
137+
* When a student leaves, you remove their seat from the list.
138+
*/
139+
public class ExamRoom_2_2 {
140+
141+
/**
142+
* TreeSet: A TreeSet is used to maintain the seats in sorted order.
143+
* This helps efficiently finding gaps between occupied seats
144+
* and determining where the next student should sit.
145+
*/
79146
private TreeSet<Integer> seats;
80147
private int n;
81148

82-
public ExamRoom_2(int n) {
149+
public ExamRoom_2_2(int n) {
83150
this.n = n;
84151
this.seats = new TreeSet<>();
85152
}
86153

154+
/**
155+
* 2. seat():
156+
*
157+
* • If no seats are occupied, the student sits at seat 0.
158+
* • Otherwise, iterate through the list of occupied seats to find the largest gap between two seats. For each gap, calculate the middle seat and check if it maximizes the distance to the nearest student.
159+
* • Finally, also consider the distance from the last occupied seat to the end of the row, in case sitting at the last seat maximizes the distance.
160+
*/
87161
public int seat() {
88162
int seat = 0;
89163

@@ -115,6 +189,9 @@ public int seat() {
115189
return seat;
116190
}
117191

192+
/**
193+
* 3. leave(int p): Simply remove seat p from the
194+
*/
118195
public void leave(int p) {
119196
seats.remove(p);
120197
}

0 commit comments

Comments
 (0)