原文链接: https://leetcode-cn.com/problems/design-parking-system
英文原文
Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.
Implement the ParkingSystem class:
ParkingSystem(int big, int medium, int small)Initializes object of theParkingSystemclass. The number of slots for each parking space are given as part of the constructor.bool addCar(int carType)Checks whether there is a parking space ofcarTypefor the car that wants to get into the parking lot.carTypecan be of three kinds: big, medium, or small, which are represented by1,2, and3respectively. A car can only park in a parking space of itscarType. If there is no space available, returnfalse, else park the car in that size space and returntrue.
Example 1:
Input ["ParkingSystem", "addCar", "addCar", "addCar", "addCar"] [[1, 1, 0], [1], [2], [3], [1]] Output [null, true, true, false, false] Explanation ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0); parkingSystem.addCar(1); // return true because there is 1 available slot for a big car parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car parkingSystem.addCar(3); // return false because there is no available slot for a small car parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied.
Constraints:
0 <= big, medium, small <= 1000carTypeis1,2, or3- At most
1000calls will be made toaddCar
中文题目
请你给一个停车场设计一个停车系统。停车场总共有三种不同大小的车位:大,中和小,每种尺寸分别有固定数目的车位。
请你实现 ParkingSystem 类:
ParkingSystem(int big, int medium, int small)初始化ParkingSystem类,三个参数分别对应每种停车位的数目。bool addCar(int carType)检查是否有carType对应的停车位。carType有三种类型:大,中,小,分别用数字1,2和3表示。一辆车只能停在carType对应尺寸的停车位中。如果没有空车位,请返回false,否则将该车停入车位并返回true。
示例 1:
输入: ["ParkingSystem", "addCar", "addCar", "addCar", "addCar"] [[1, 1, 0], [1], [2], [3], [1]] 输出: [null, true, true, false, false] 解释: ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0); parkingSystem.addCar(1); // 返回 true ,因为有 1 个空的大车位 parkingSystem.addCar(2); // 返回 true ,因为有 1 个空的中车位 parkingSystem.addCar(3); // 返回 false ,因为没有空的小车位 parkingSystem.addCar(1); // 返回 false ,因为没有空的大车位,唯一一个大车位已经被占据了
提示:
0 <= big, medium, small <= 1000carType取值为1,2或3- 最多会调用
addCar函数1000次
通过代码
高赞题解
简单变量
一个简单的做法是,直接使用几个成员变量来记录。
[]class ParkingSystem { int big, medium, small; public ParkingSystem(int _big, int _medium, int _small) { big = _big; medium = _medium; small = _small; } public boolean addCar(int ct) { if (ct == 1 && big > 0) return big-- > 0; else if (ct == 2 && medium > 0) return medium-- > 0; else if (ct == 3 && small > 0) return small-- > 0; return false; } }
- 时间复杂度:$O(1)$
- 空间复杂度:$O(1)$
哈希表
另外一个更好拓展的方法,使用哈希表来进行记录。
这样做的好处是,当增加车类型,只需要重载一个构造方法即可。
[]class ParkingSystem { Map<Integer, Integer> map = new HashMap<>(); public ParkingSystem(int _big, int _medium, int _small) { map.put(1, _big); map.put(2, _medium); map.put(3, _small); } public boolean addCar(int ct) { if (map.get(ct) > 0) { map.put(ct, map.get(ct) - 1); return true; } return false; } }
- 时间复杂度:$O(1)$
- 空间复杂度:$O(1)$
二进制分段
事实上,由于 $1000$ 的二进制表示只有 $10$ 位,而 $int$ 有 $32$ 位。
我们可以使用一个 $int$ 配合「位运算」来分段做。
使用 $[0,10)$ 代表 big,$[10,20)$ 表示 medium,$[20,30)$ 表示 small
PS. 这样 $int$ 分段的做法,在工程源码上也有体现:JDK 中的 ThreadPoolExecutor 使用了一个 $ctl$ 变量 ($int$ 类型) 的前 $3$ 位记录线程池的状态,后 $29$ 位记录程池中线程个数。
这样的「二进制分段压缩存储」的主要目的,不是为了减少使用一个 $int$,而是为了让「非原子性操作」变为「原子性操作」。
我们可以分析下为什么 ThreadPoolExecutor 要这么做。
当线程数量变化为某个特定值时,要修改的就不仅仅是「线程数量」,还需要修改「线程池的状态」。
由于并发环境下,如果要做到「原子性」地同时需要修改两个 $int$ 的话。只能上「重量级锁」,「重量级锁」就会涉及到「内核态」的系统调用,通常是耗时是「用户态」的上百倍。
但是如果我们将「线程数量」和「线程池的状态」合二为一之后,我们只需要修改一个 $int$,这时候只需要使用 CAS 做法(用户态)即可保证线程安全与原子性。
那么对应到该题,如果我们允许同时停入不同类型的车,在不引入重量级锁的前提下,想要真正做到「同时」修改两种类型的车的车位的话,只能采用这样的「二进制分段」做法 ~
[]class ParkingSystem { int cnt; // [small medium big] public ParkingSystem(int _big, int _medium, int _small) { for (int i = 0; i < 30; i++) { int cur = 0; if (i < 10) { cur = (_big >> i) & 1; } else if (i < 20) { cur = (_medium >> (i - 10)) & 1; } else if (i < 30) { cur = (_small >> (i - 20)) & 1; } cnt += cur == 1 ? (1 << i) : 0; } } public boolean addCar(int ct) { int cur = countOfType(ct); if (cur > 0) { setCount(ct, cur - 1); return true; } return false; } int countOfType(int ct) { int ans = 0; int start = --ct * 10, end = start + 10; for (int i = start; i < end; i++) { if (((cnt >> i) & 1) == 1) { ans += (1 << (i - start)); } } return ans; } void setCount(int ct, int pc) { int start = --ct * 10, end = start + 10; for (int i = start; i < end; i++) { if (((pc >> (i - start)) & 1) == 1) { cnt |= (1 << i); } else { cnt &= ~(1 << i); } } } }
- 时间复杂度:$O(1)$
- 空间复杂度:$O(1)$
最后
如果有帮助到你,请给题解点个赞和收藏,让更多的人看到 ~ (“▔□▔)/
如有不理解的地方,欢迎你在评论区给我留言,我都会逐一回复 ~
也欢迎你 关注我 和 加入我们的「组队打卡」小群 ,提供写「证明」&「思路」的高质量题解
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 58367 | 69156 | 84.4% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|