-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class ExamRoom { | ||
int N; | ||
TreeSet<Integer> students; | ||
|
||
public ExamRoom(int N) { | ||
this.N = N; | ||
students = new TreeSet(); | ||
} | ||
|
||
public int seat() { | ||
int student = 0; | ||
if (students.size() > 0) { | ||
int dist = students.first(); | ||
Integer prev = null; | ||
for (Integer s: students) { | ||
if (prev != null) { | ||
int d = (s - prev) / 2; | ||
if (d > dist) { | ||
dist = d; | ||
student = prev + d; | ||
} | ||
} | ||
prev = s; | ||
} | ||
|
||
// Considering the right-most seat. | ||
if (N - 1 - students.last() > dist) | ||
student = N - 1; | ||
} | ||
|
||
students.add(student); | ||
return student; | ||
} | ||
|
||
public void leave(int p) { | ||
students.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); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters