/* Problem 855. Exam Room Medium There is an exam room with n seats in a single row labeled from 0 to n - 1. 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. Design a class that simulates the mentioned exam room. Implement the ExamRoom class: ExamRoom(int n) Initializes the object of the exam room with the number of the seats n. int seat() Returns the label of the seat at which the next student will set. 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. Example 1: Input ["ExamRoom", "seat", "seat", "seat", "seat", "leave", "seat"] [[10], [], [], [], [], [4], []] Output [null, 0, 9, 4, 2, null, 5] Explanation ExamRoom examRoom = new ExamRoom(10); examRoom.seat(); // return 0, no one is in the room, then the student sits at seat number 0. examRoom.seat(); // return 9, the student sits at the last seat number 9. examRoom.seat(); // return 4, the student sits at the last seat number 4. examRoom.seat(); // return 2, the student sits at the last seat number 2. examRoom.leave(4); examRoom.seat(); // return 5, the student sits at the last seat number 5. Constraints: 1 <= n <= 109 It is guaranteed that there is a student sitting at seat p. At most 104 calls will be made to seat and leave. */ #include using namespace std; class ExamRoom { public: enum Seat {Empty, Taken}; map seats; int capacity = 0; ExamRoom(int n) { capacity = n; for (int i = 0; i < n; i++) seats.insert({i,Empty}); } // pos, distance examples. Seat 0 and last seat pos are taken. Distance // count starts at zero. // // (1) dist: 0 1 2 3 4 5 6 7 (2) dist: 0 1 2 3 4 5 6 7 8 // --------------- ----------------- // seat: 0 1 2 3 4 5 6 7 8 9 seat: 0 1 2 3 4 5 6 7 8 9 // x x x // // (3) dist: 0 1 2 (4) dist: 0 1 (5) dist: 0 // --------- ------- ----- // seat: 0 1 2 3 4 seat: 0 1 2 3 seat: 0 1 2 // x x x x x x // // (1) new seat = pos + dist/2 = 1 + 7/2 = 1 + 3 = 4 // (2) if last open seat is the end seat then don't divide distance by 2 // new seat = pos + dist = 1 + 8 = 9 // (3) new seat = pos + dist/2 = 1 + 2/2 = 2 // (4) new seat = pos + dist/2 = 1 + 1/2 = 1 // (5) new seat = pos + dist/2 = 1 + 0/2 = 1 // Above not correct this scenario all have distances of 2 to the // nearest seat so seat #2 should be chosen. Need to check distances // from first empty seat and last empty seat // // dist: 0 1 2 3 4 5 6 7 // --------------- // seat: 0 1 2 3 4 5 6 7 8 9 // x ^ x ^ ^ x // | | | // | | | int seat() { // maintain map of distance -> position. If there already exists // a distance entry in the map don't insert a new one since the // start pos will be greater than the existing one. This is to // satisfy condition if there are multiple seats with the same // distance find distance with lowest seat number map cache; int distance = 0; int pos = 0; // find first empty seat for (auto i: seats) { if (i.second == Taken) { pos++; continue; } else break; } // special case, all seats empty if (pos == 0) { seats.at(0) = Taken; return 0; } for (int i = pos; i < (int)seats.size(); i++) { if (seats.at(i) == Empty) { // special case for last seat position empty if (i == (int)seats.size() - 1) cache.insert({distance*2,pos}); distance += 1; } else { if (!cache.count(distance)) cache.insert({distance,pos}); distance = 0; pos = i; } } // last cache entry will have max distance { auto iter = cache.rbegin(); int distance = iter->first; int pos = iter->second; int seat = pos + distance/2; seats.at(seat) = Taken; return seat; } } void leave(int p) { seats.at(p) = Empty; } }; int main(void) { vector output; ExamRoom er(10); output.push_back("null"); output.push_back(to_string(er.seat())); output.push_back(to_string(er.seat())); output.push_back(to_string(er.seat())); output.push_back(to_string(er.seat())); er.leave(4); output.push_back("null"); output.push_back(to_string(er.seat())); cout << " result: ["; for (size_t i = 0; i < output.size(); i++) cout << output.at(i) << (i < output.size() - 1 ? ", " : ""); cout << "]\n"; cout << "expected: [null, 0, 9, 4, 2, null, 5]\n"; }