• [LeetCode] 1845. Seat Reservation Manager


    Design a system that manages the reservation state of n seats that are numbered from 1 to n.

    Implement the SeatManager class:

    • SeatManager(int n) Initializes a SeatManager object that will manage n seats numbered from 1 to n. All seats are initially available.
    • int reserve() Fetches the smallest-numbered unreserved seat, reserves it, and returns its number.
    • void unreserve(int seatNumber) Unreserves the seat with the given seatNumber.

    Example 1:

    Input
    ["SeatManager", "reserve", "reserve", "unreserve", "reserve", "reserve", "reserve", "reserve", "unreserve"]
    [[5], [], [], [2], [], [], [], [], [5]]
    Output
    [null, 1, 2, null, 2, 3, 4, 5, null]
    
    Explanation
    SeatManager seatManager = new SeatManager(5); // Initializes a SeatManager with 5 seats.
    seatManager.reserve();    // All seats are available, so return the lowest numbered seat, which is 1.
    seatManager.reserve();    // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
    seatManager.unreserve(2); // Unreserve seat 2, so now the available seats are [2,3,4,5].
    seatManager.reserve();    // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
    seatManager.reserve();    // The available seats are [3,4,5], so return the lowest of them, which is 3.
    seatManager.reserve();    // The available seats are [4,5], so return the lowest of them, which is 4.
    seatManager.reserve();    // The only available seat is seat 5, so return 5.
    seatManager.unreserve(5); // Unreserve seat 5, so now the available seats are [5].

    Constraints:

    • 1 <= n <= 105
    • 1 <= seatNumber <= n
    • For each call to reserve, it is guaranteed that there will be at least one unreserved seat.
    • For each call to unreserve, it is guaranteed that seatNumber will be reserved.
    • At most 105 calls in total will be made to reserve and unreserve.

    座位预约管理系统。

    请你设计一个管理 n 个座位预约的系统,座位编号从 1 到 n 。

    请你实现 SeatManager 类:

    SeatManager(int n) 初始化一个 SeatManager 对象,它管理从 1 到 n 编号的 n 个座位。所有座位初始都是可预约的。
    int reserve() 返回可以预约座位的 最小编号 ,此座位变为不可预约。
    void unreserve(int seatNumber) 将给定编号 seatNumber 对应的座位变成可以预约。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/seat-reservation-manager
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这是一道设计题,我这里提供一个最小堆的做法。注意题目标红的条件,unreserve的座位号一定是之前已经被reserve过的。没有这个条件,这个题是没法做的。我直接给代码了。

    时间O(nlogn)

    空间O(n)

    Java实现

     1 class SeatManager {
     2     PriorityQueue<Integer> minHeap;
     3 
     4     public SeatManager(int n) {
     5         minHeap = new PriorityQueue<>();
     6         for (int i = 1; i <= n; i++) {
     7             minHeap.offer(i);
     8         }
     9     }
    10 
    11     public int reserve() {
    12         if (!minHeap.isEmpty()) {
    13             return minHeap.poll();
    14         }
    15         return -1;
    16     }
    17 
    18     public void unreserve(int seatNumber) {
    19         minHeap.offer(seatNumber);
    20     }
    21 }
    22 
    23 /**
    24  * Your SeatManager object will be instantiated and called as such:
    25  * SeatManager obj = new SeatManager(n);
    26  * int param_1 = obj.reserve();
    27  * obj.unreserve(seatNumber);
    28  */

    LeetCode 题目总结

  • 相关阅读:
    神经网络的数学推导
    矩阵乘法的梯度计算
    深入理解设计矩阵(Design Matrix)
    拉格朗日乘子
    PRML中文版(马春鹏)勘误表
    增强学习笔记 第三章 马尔科夫决策过程
    贝叶斯统计推断的阅读笔记
    Kalman Filter的数学推导
    线性代数随笔(二):矩阵和向量乘法
    线性代数随笔(一):线性变换,特征分解,二次型
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14725188.html
Copyright © 2020-2023  润新知