|
46 | 46 | */ |
47 | 47 |
|
48 | 48 | import java.util.TreeSet; |
| 49 | +import java.util.ArrayList; |
| 50 | +import java.util.Collections; |
49 | 51 |
|
50 | 52 | /** |
51 | 53 | * Your ExamRoom object will be instantiated and called as such: |
@@ -73,17 +75,89 @@ public class ExamRoom { |
73 | 75 |
|
74 | 76 | // V1 |
75 | 77 |
|
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 | + */ |
79 | 146 | private TreeSet<Integer> seats; |
80 | 147 | private int n; |
81 | 148 |
|
82 | | - public ExamRoom_2(int n) { |
| 149 | + public ExamRoom_2_2(int n) { |
83 | 150 | this.n = n; |
84 | 151 | this.seats = new TreeSet<>(); |
85 | 152 | } |
86 | 153 |
|
| 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 | + */ |
87 | 161 | public int seat() { |
88 | 162 | int seat = 0; |
89 | 163 |
|
@@ -115,6 +189,9 @@ public int seat() { |
115 | 189 | return seat; |
116 | 190 | } |
117 | 191 |
|
| 192 | + /** |
| 193 | + * 3. leave(int p): Simply remove seat p from the |
| 194 | + */ |
118 | 195 | public void leave(int p) { |
119 | 196 | seats.remove(p); |
120 | 197 | } |
|
0 commit comments