[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 ...
随机推荐
- shell判断文件是否存在
转自:http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html 1. shell判断文件,目录是否存在或者具有权限 2. #!/bi ...
- Axure 全局辅助线(转)
普通辅助线作用于当前页 全局作用于所有页面 , 包括新建页面 创建普通辅助线直接拉出来 创建全局辅助线 , 在拉出来的时候按住 Ctrl 默认情况下 , 颜色不同 辅助线可以多选 , 用拖选 或 按 ...
- MongoDB学习(1)—在Windows系统中安装MongoDB
概述 本文主要介绍在Windows系统安装MongoDB的方法. MongoDB官方网址:http://www.mongodb.org/,最新版本为2.6.7. 注意: 从2.2版本开始,MongoD ...
- HDU 5791 Two DP
Two Problem Description Alice gets two sequences A and B. A easy problem comes. How many pair of ...
- javase基础笔记4——异常/单例和类集框架
继承 extends final关键 多态 是在继承的基础上 接口 interface 异常 exception 包的访问可控制权限 private default protect public 异常 ...
- 程序员必懂:javaweb三大框架知识点总结
原文链接:http://www.cnblogs.com/SXTkaifa/p/5968631.html javaweb三大框架知识点总结 一.Struts2的总结 1.Struts 2的工作流程,从请 ...
- ubuntu14使用qemu调试linux内核
# 下载内核源代码编译内核 cd ~/LinuxKernel/ wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.6.tar.x ...
- github 使用网址
[Github教程]史上最全github使用方法:github入门到精通 http://blog.csdn.net/hcbbt/article/details/11651229 githup的使用 h ...
- JAVA Day7
6 方法 1.格式[访问控制符] void返回值类型 方法名(参数列表:数据类型 参数名); 2.类的方法: *用来定义类的某种行为或功能 * 3.方法的返回值 *如果有返回值,方法中必须要使用 ...
- 如何在Crystal Portlet中正确返回JSON数据给AJAX请求?
当Crystal Portlet中需要采用Ajax请求,并让后台返回Json数据时,如何才能正确.方便的返回Json数据呢? 以下两种方法均可: 方法一:Ajax请求时,采用RenderURL,对应P ...