ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 1845. Seat Reservation Manager

2021-05-02 02:03:59  阅读:445  来源: 互联网

标签:seatManager SeatManager unreserve seat Manager seats Reservation LeetCode reserv


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 题目总结

标签:seatManager,SeatManager,unreserve,seat,Manager,seats,Reservation,LeetCode,reserv
来源: https://www.cnblogs.com/cnoodle/p/14725188.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有