Class SeatManager

java.lang.Object
g1801_1900.s1845_seat_reservation_manager.SeatManager

public class SeatManager extends Object
1845 - Seat Reservation Manager.<p>Medium</p> <p>Design a system that manages the reservation state of <code>n</code> seats that are numbered from <code>1</code> to <code>n</code>.</p> <p>Implement the <code>SeatManager</code> class:</p> <ul> <li><code>SeatManager(int n)</code> Initializes a <code>SeatManager</code> object that will manage <code>n</code> seats numbered from <code>1</code> to <code>n</code>. All seats are initially available.</li> <li><code>int reserve()</code> Fetches the <strong>smallest-numbered</strong> unreserved seat, reserves it, and returns its number.</li> <li><code>void unreserve(int seatNumber)</code> Unreserves the seat with the given <code>seatNumber</code>.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input</strong> [&ldquo;SeatManager&rdquo;, &ldquo;reserve&rdquo;, &ldquo;reserve&rdquo;, &ldquo;unreserve&rdquo;, &ldquo;reserve&rdquo;, &ldquo;reserve&rdquo;, &ldquo;reserve&rdquo;, &ldquo;reserve&rdquo;, &ldquo;unreserve&rdquo;] [[5], [], [], [2], [], [], [], [], [5]]</p> <p><strong>Output:</strong> [null, 1, 2, null, 2, 3, 4, 5, null]</p> <p><strong>Explanation:</strong></p> <p>SeatManager seatManager = new SeatManager(5); // Initializes a SeatManager with 5 seats.</p> <p>seatManager.reserve(); // All seats are available, so return the lowest numbered seat, which is 1.</p> <p>seatManager.reserve(); // The available seats are [2,3,4,5], so return the lowest of them, which is 2.</p> <p>seatManager.unreserve(2); // Unreserve seat 2, so now the available seats are [2,3,4,5].</p> <p>seatManager.reserve(); // The available seats are [2,3,4,5], so return the lowest of them, which is 2.</p> <p>seatManager.reserve(); // The available seats are [3,4,5], so return the lowest of them, which is 3.</p> <p>seatManager.reserve(); // The available seats are [4,5], so return the lowest of them, which is 4.</p> <p>seatManager.reserve(); // The only available seat is seat 5, so return 5.</p> <p>seatManager.unreserve(5); // Unreserve seat 5, so now the available seats are [5].</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 10<sup>5</sup></code></li> <li><code>1 <= seatNumber <= n</code></li> <li>For each call to <code>reserve</code>, it is guaranteed that there will be at least one unreserved seat.</li> <li>For each call to <code>unreserve</code>, it is guaranteed that <code>seatNumber</code> will be reserved.</li> <li>At most <code>10<sup>5</sup></code> calls <strong>in total</strong> will be made to <code>reserve</code> and <code>unreserve</code>.</li> </ul>
  • Constructor Details

    • SeatManager

      public SeatManager(int n)
  • Method Details

    • reserve

      public int reserve()
    • unreserve

      public void unreserve(int seatNumber)