[LintCode] Parking Lot 停车场问题
Design a parking lot.
see CC150 OO Design for details.
1) n
levels, each level has m
rows of spots and each row has k
spots.So each level has m
x k
spots.
2) The parking lot can park motorcycles, cars and buses
3) The parking lot has motorcycle spots, compact spots, and large spots
4) Each row, motorcycle spots id is in range[0,k/4)(0 is included, k/4 is not included)
, compact spots id is in range [k/4,k/4*3)
and large spots id is in range [k/4*3,k)
.
5) A motorcycle can park in any spot
6) A car park in single compact spot or large spot
7) A bus can park in five large spots that are consecutive and within same row. it can not park in small spots
level=1, num_rows=1, spots_per_row=11
parkVehicle("Motorcycle_1") // return true
parkVehicle("Car_1") // return true
parkVehicle("Car_2") // return true
parkVehicle("Car_3") // return true
parkVehicle("Car_4") // return true
parkVehicle("Car_5") // return true
parkVehicle("Bus_1") // return false
unParkVehicle("Car_5")
parkVehicle("Bus_1") // return true
CareerCup上的原题,请参见我之前的博客8.4 Parking Lot 停车场问题。
LintCode的这道题的C++的OJ应该有问题,因为我的代码在本地调试都正确,不知道为何通过不了OJ,有没有大神持有同样的观点?
// enum type for Vehicle
enum class VehicleSize {
Motorcycle,
Compact,
Large
}; class Vehicle {
public:
virtual VehicleSize size() {}
virtual int spot_num() {}
}; class Bus: public Vehicle {
public:
VehicleSize size() {
return VehicleSize::Large;
}
int spot_num() {
return ;
}
}; class Car: public Vehicle {
public:
VehicleSize size() {
return VehicleSize::Compact;
}
int spot_num() {
return ;
}
}; class Motorcycle: public Vehicle {
public:
VehicleSize size() {
return VehicleSize::Motorcycle;
}
int spot_num() {
return ;
}
}; class Level {
public:
Level(int num_rows, int spots_per_row) {
int moto = spots_per_row / ;
moto_spots.resize(moto);
int car = spots_per_row / * ;
compact_spots.resize(car - moto);
large_spots.resize(spots_per_row - car);
} bool park_vehicle(Vehicle* vehicle, VehicleSize size, int num) {
if (size == VehicleSize::Motorcycle) {
for (int i = ; i < moto_spots.size(); ++i) {
if (moto_spots[i] == NULL) {
moto_spots[i] = vehicle;
vehicle_to_spot[vehicle][VehicleSize::Motorcycle] = i;
return true;
}
}
return false;
} else if (size == VehicleSize::Compact) {
for (int i = ; i < compact_spots.size(); ++i) {
if (compact_spots[i] == NULL) {
compact_spots[i] = vehicle;
vehicle_to_spot[vehicle][VehicleSize::Compact] = i;
return true;
}
}
for (int i = ; i < large_spots.size(); ++i) {
if (large_spots[i] == NULL) {
large_spots[i] = vehicle;
vehicle_to_spot[vehicle][VehicleSize::Large] = i;
return true;
}
}
return false;
} else if (size == VehicleSize::Large) {
for (int i = ; i < large_spots.size(); ++i) {
if (large_spots[i] == NULL) {
bool can_park = true;
for (int j = i; j < i + num; ++j) {
if (large_spots[j] != NULL) {
can_park = false;
break;
}
}
if (can_park) {
for (int j = i; j < i + num; ++j) {
large_spots[j] = vehicle;
}
vehicle_to_spot[vehicle][VehicleSize::Large] = i;
return true;
}
}
}
return false;
}
} void unpark_vehicle(Vehicle *vehicle) {
map<VehicleSize, int> spot = vehicle_to_spot[vehicle];
VehicleSize size = vehicle->size();
if (spot.count(size)) {
int idx = spot[size];
if (size == VehicleSize::Motorcycle) {
moto_spots[idx] = NULL;
} else if (size == VehicleSize::Compact) {
compact_spots[idx] = NULL;
} else {
for (int i = idx; i < large_spots.size(); ++i) {
if (large_spots[i] == vehicle) {
large_spots[i] = NULL;
} else {
break;
}
}
}
} else if (size == VehicleSize::Compact && spot.count(VehicleSize::Large)) {
int idx = spot[VehicleSize::Large];
large_spots[idx] = NULL;
}
} private:
vector<Vehicle*> moto_spots;
vector<Vehicle*> compact_spots;
vector<Vehicle*> large_spots;
map<Vehicle*, map<VehicleSize, int>> vehicle_to_spot;
}; class ParkingLot {
public:
// @param n number of leves
// @param num_rows each level has num_rows rows of spots
// @param spots_per_row each row has spots_per_row spots
ParkingLot(int n, int num_rows, int spots_per_row) {
for (int i = ; i < n; ++i) {
Level *level = new Level(num_rows, spots_per_row);
levels.push_back(level);
}
} // Park the vehicle in a spot (or multiple spots)
// Return false if failed
bool parkVehicle(Vehicle &vehicle) {
for (int i = ; i < levels.size(); ++i) {
if (levels[i]->park_vehicle(&vehicle, vehicle.size(), vehicle.spot_num())) {
vehicle_to_level[&vehicle] = levels[i];
return true;
}
}
return false;
} // unPark the vehicle
void unParkVehicle(Vehicle &vehicle) {
Level *level = vehicle_to_level[&vehicle];
if (level) {
level->unpark_vehicle(&vehicle);
}
}
private:
vector<Level*> levels;
map<Vehicle*, Level*> vehicle_to_level;
};
参考资料:
http://www.jianshu.com/p/2bd60b69393d
[LintCode] Parking Lot 停车场问题的更多相关文章
- [CareerCup] 8.4 Parking Lot 停车场问题
8.4 Design a parking lot using object-oriented principles. LintCode上的原题,请参见我的另一篇博客Parking Lot 停车场问题. ...
- English trip V1 - 2.Don't Do That Teacher:Patrick Key: 祈使句(imperatives)
什么是祈使句? What's imperatives? 求或者希望别人做什么事或者不做什么事时用的句子:带有命令的语气 In this lesson you will learn how to ...
- 新概念英语三 新东方主讲Lesson1
新概念二 Lesson95 词汇 ①get a shock 吓了一跳,得到一个惊喜 例:his wife got a shock get into a such mess 这么不幸搞得一片狼籍弄得这样 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- [PA2014]Parking
[PA2014]Parking 题目大意: 停车场是一个宽度为\(w(w\le10^9)\)的矩形.我们以其左下角顶点为原点,坐标轴平行于矩形的边,建立直角坐标系.停车场很长,我们可以认为它一直向右边 ...
- bzoj3718 [PA2014]Parking
Description 你的老板命令你将停车场里的车移动成他想要的样子.停车场是一个长条矩形,宽度为w.我们以其左下角顶点为原点,坐标轴平行于矩形的边,建立直角坐标系.停车场很长,我们可以认为它一直向 ...
- C语言实现简单的停车场管理系统
问题描述:停车场是一个能放n辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放.若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入.当停车场中的车离开时,由于通道窄,在它后面呢 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
随机推荐
- 物化视图刷新慢--有可能是mv log被多个mv使用造成的
同事说物化视图刷新慢,经检生产环境,发现部分物化视图刷新慢的原因是:由于同一个物化视图日志(mv log)被多个物化视图(mv)使用,不同的物化视图(mv)使用不同的刷新间隔,导致物化视图日志(mv ...
- python学习第二天
dict字典 把数据放入dict:直接赋值.初始化时指定 pop删除key set集合 add添加元素 remove删除元素 字符串str是不可变对象,对字符串的操作都会返回新的字符串 pass 什么 ...
- Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab
今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...
- POJ 2750 Potted Flower (线段树区间合并)
开始懵逼找不到解法,看了网上大牛们的题解才发现是区间合并... 给你n个数形成一个数列环,然后每次进行一个点的修改,并输出这个数列的最大区间和(注意是环,并且区间最大只有n-1个数) 其实只需要维护 ...
- Sigar介绍与使用
Sigar是Hyperic-hq产品的基础包,是Hyperic HQ主要的数据收集组件.它用来从许多平台收集系统和处理信息. 这些平台包括:Linux, Windows, Solaris, AIX, ...
- DSP using MATLAB 示例 Example3.12
用到的性质 代码: n = -5:10; x = sin(pi*n/2); k = -100:100; w = (pi/100)*k; % freqency between -pi and +pi , ...
- react-基础(2)
表单相关 固定的几个属性和事件 value: <input>,<textarea>; checked: <input> typeof checkbox, radio ...
- 数据库查询Database中的表
public class UserDA { SqlConnection conn; SqlCommand cmd; public UserDA(Use uuu) { conn =new SqlConn ...
- PHP 不使用第三个变量实现交换两个变量的值
//字符串版本 结合使用substr,strlen两个方法实现$a="a";$b="b";echo '交换前 $a:'.$a.',$b:'.$b.'<br ...
- 8611 大牛之路I
时间限制:500MS 内存限制:1000K 提交次数:157 通过次数:62 题型: 编程题 语言: C++;C Description 要成为ACM大牛,要掌握很多必需的知识点.某些知识点可以 ...