Question: At a bus-station, you have time-table for buses arrival and departure. You need to find the minimum number of platforms so that all the buses can be accommodated as per their schedule.
Example: Time table is like below:
Bus Arrival Departure BusA 0900 hrs 0930 hrs BusB 0915 hrs 1300 hrs BusC 1030 hrs 1100 hrs BusD 1045 hrs 1145 hrs
Then the answer must be 3. Otherwise the bus-station will not be able to accommodate all the buses.
Answer:
Let’s take the same example as described above. Now if we apply dynamic programming and calculate the number of buses at station at any time (when a bus comes or leaves). Maximum number in that pool will be nothing but the maximum number of buses at the bus-station at any time. That number is the minimum number of platforms required.
So first sort all the arrival(A) and departure(D) time in an int array. Please save the corresponding arrival or departure in the array also. Either you can use a particular bit for this purpose or make a structure. After sorting our array will look like this:
0900 0915 1930 1030 1045 1100 1145 1300 A A D A A D D D
Now modify the array as put 1 where you see A and -1 where you see D. So new array will be like this:
1 1 -1 1 1 -1 -1 -1
And finally make a cumulative array out of this:
1 2 1 2 3 2 1 0
Your solution will be the maximum value in this array. Here it is 3.
I think that code for this will not be complex so I am skipping that part. The complexity of this solution depends on the complexity of sorting.
PS: If you have a arriving and another departing at same time then put departure time first in sorted array.
Examples:
Input: arr[] = {9:00, 9:40, 9:50, 11:00, 15:00, 18:00} dep[] = {9:10, 12:00, 11:20, 11:30, 19:00, 20:00} Output: 3 There are at-most three trains at a time (time between 11:00 to 11:20)
// Returns minimum number of platforms reqquired int findPlatform(int arr[], int dep[], int n) { // Sort arrival and departure arrays sort(arr, arr+n); sort(dep, dep+n); // plat_needed indicates number of platforms needed at a time int plat_needed = 1, result = 1; int i = 1, j = 0; // Similar to merge in merge sort to process all events in sorted order while (i < n && j < n) { // If next event in sorted order is arrival, increment count of // platforms needed if (arr[i] < dep[j]) { plat_needed++; i++; if (plat_needed > result) // Update result if needed result = plat_needed; } else // Else decrement count of platforms needed { plat_needed--; j++; } } return result; }
Time Complexity: O(nLogn), assuming that a O(nLogn) sorting algorithm for sorting arr[] and dep[].
或者下面这种写法,思路是一样的:
int min_rooms(vector<Interval>& meetings) { vector<int> times; for(auto m : meetings) { times.push_back(m.begin); times.push_back(-m.end); } sort(times.begin(), times.end(), [](int a, int b){ return abs(a) == abs(b) ? a < b : abs(a) < abs(b); }); int ret = 0, cur = 0; for(auto t : times) { if(t >= 0) ret = max(ret, ++cur); else --cur; } return ret; }
The follow up question:
You have a number of meetings (with their start and end times). You need to schedule them using the minimum number of rooms. Return the list of meetings in every room.
The answer:
First we need to realize that randomly schedule meetings to available rooms won’t give you an optimal solution. For example, suppose there are 4 meetings, the (start, end) time are (1, 3), (1, 5), (6, 7), (4, 7). When (1, 3) comes, assign to room A, then (1, 5) comes, assign to room B, then (6, 7) comes, assign to room A as it is available, then (4, 7) comes, both room A and B are not available so we have to assign a new room C for it. However, a better solution is two rooms, room A for meeting (1, 3) (4, 7) and room B for meeting (1, 5) (6, 7).
However, the optimal solution solution is not far from it. If we first sort all the meeting by the start time, then we could just assign them one by one and to the first available room.
The reason is simple, when considering a meeting from s[i] to e[i], as there is no unschedule meeting before s[i], by putting the (s[i], e[i]) meeting in any available room (if there is one) would leads to the same results.
So the code looks like this.
void arrange(vector<pair> meeting) { // the pair contains the start and end time. sort(meeting.begin(), meeting.end()); vector<vector<pair>> room; // for each room, there is a list of meetings. for (auto m : meeting) { bool found = False; for (auto& r : room) { if (m.first >= r.back().second) // we can arrange the meeting in the room r.push_back(m); found = True; break; } } if (!found) { room.push_back({m}); } } cout << "Requires " << room.size() << " rooms" << endl; for (int i = 0; i < room.size(); ++i) { cout << "Room " << i << endl; for (auto m : room[i]) { cout << "Meeting: " << m.first << " " << m.second << endl; } } }
References:
http://tech-queries.blogspot.com/2009/05/number-of-bus-stations.html
http://www.geeksforgeeks.org/minimum-number-platforms-required-railwaybus-station/
http://www.fgdsb.com/2015/01/30/meeting-rooms/
https://nuttynanaus.wordpress.com/2014/04/26/software-engineer-interview-questions-3/
https://hellosmallworld123.wordpress.com/2014/05/30/arranging-the-meeting-room/
相关推荐
space and minimum number of external components. All functions are controlled through a simple 3-wire serial interface or I2C serial interface. The device operates from a power supply of 2.7 – 5.5 ...
勇敢的集体货运网站: : 前端在webroot/js...stations/generate.py 中调整区域过滤器/站列表在./gen_stations/运行./cron.sh后端 - 合约在./gen_contracts/cron.sh 中编辑公司 API 密钥在./gen_contracts/运行./cron.sh
when stations (STAs)/AP that operate in 20/40 MHz BSS are able to dynamically switch between 20 MHz and 40 MHz transmit/receive modes and CCA is used in the extension channel, the throughput of both ...
地面城市交通的站点地图 为应用程序“地面城市交通的站点地图”提供了现成的模板和... import_stations控制命令从moscow_bus_stations.csv文件加载数据 将数据从csv加载到db的命令 python manage.py import_stations
/api/v1/stations/<string>/<int>/ 查询车辆到站信息 /api/v1/stops/<string>/<int>/ 示例: 查询公交100上行站台信息 /api/v1/stations/up/110/ { "data": { "1": "吉浦路仁德路", "2": "武川路武东路", "3":...
租用端点: PATCH / api / stations / borrow / {rentStationId} / {UserId} / {bikeSerialNumber} 归还自行车的端点: PATCH / api / stations / gback / {backStationId} / {UserId} BUILD'n'RUN项目: 对于Unix...
curl https://raw.githubusercontent.com/capitainetrain/stations/master/stations.csv > stations.csv 将stations.csv文件通过管道stations.csv到脚本中,并将生成的 GeoJSON 传输到一个新文件中: stations2...
/ stations /:id /到达 特定车站的详细信息,仅到达 / stations /:id /出发 特定车站的详细信息(仅出发) /路线 要求从根特圣皮特斯到安特卫普中央的路线 本地化和i18n 通过在请求中提供定义的Accept-Language...
Numerical Calculation of the Impact of Offshore Wind Power Stations on Hydrodynamic Conditions,张玮,夏海峰,本文通过建立长江口、杭州湾及其附近海域大范围平面二维潮流数学模型,探讨上海风力发电场规划...
Zurich relative sunspot number R, the solar radio noise flux $ at 10-7 cm and the ionospheric index of solar activity IF2, and numerical expressions are derived. There is no difference in the degree ...
本文《Deployment Analysis of cooperative OFDM base stations》深入探讨了如何在OFDM系统中通过合作游戏理论解决资源分配问题,以实现整体系统速率的最大化,同时确保用户间的公平性。 **关键词:**OFDM,合作...
比利时铁路 2 停止时间该项目... 1 停止时间看起来像这样: { "stop" : "http://irail.be/stations/NMBS/008821121#10" , "departureTime" : "2014-12-15T18:02:00Z" , "headsign" : "Antwerpen-Centraal" , "@id" : ...
空气排放 链接: : ... 使用方法:在链接之后输入您要从中获取数据的路径 example -->...to get all stations data 喂养 ./feed/stations GET->..../feed/stations/:ID GET->通过_ID获取特定的电台查询 认证 ./auth/s
解压"JBC Stations Updater With Maintenance U03.3106 Installer.zip"后,运行其中的"JBC Stations Updater With Maintenance U03.3106 Installer.exe"程序,用户界面会引导用户进行下一步操作。在连接焊台至电脑后...
"Python库 | covmatic-stations-2.12.8.tar.gz" 是一个与Python相关的压缩包文件,其中包含了名为 "covmatic-stations-2.12.8" 的库的源代码。这个库可能专注于特定的功能或服务,可能是为了自动化、测试、数据处理...
Antennas for Base Stations in Wireless Communications,通信基站经典天线设计,是通信天线工程师必不可少的一本书。
AbfahrtAPI 这是一个用 node.js 编写的小 API,它提供了对图宾根所有公交车站当前发车时间的访问。 API 端点 ...GET /stations/{id}/departures #Returns an JSON array with the next departures