给定到达火车站的所有火车的到达和离开时间,任务是找到火车站所需的最少平台数目,以确保没有火车在等待。
我们获得了两个数组,分别代表了停靠的火车的到达和离开时间。
例子:
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
Explanation: There are at-most three trains at a time (time between 11:00 to 11:20)
Input: arr[] = {9:00, 9:40}
dep[] = {9:10, 12:00}
Output: 1
Explanation: Only one platform is needed.
天真的解决方案:
- 方法:想法是每个间隔一个接一个地找出与之重叠的间隔数。跟踪与间隔重叠的最大间隔数。最后,返回最大值。
- 算法:
- 运行两个嵌套循环,外循环从开始到结束,内循环从i + 1到结束。
- 对于外循环的每次迭代,请找到与当前间隔相交的间隔计数。
- 在外循环的每次迭代中,使用最大重叠数更新答案。
- 打印答案。
执行:
CPP14
// Program to find minimum number of platforms
// required on a railway station
#include
#include
using namespace std;
// Returns minimum number of platforms reqquired
int findPlatform(int arr[], int dep[], int n)
{
// plat_needed indicates number of platforms
// needed at a time
int plat_needed = 1, result = 1;
int i = 1, j = 0;
// run a nested loop to find overlap
for (int i = 0; i < n; i++) {
// minimum platform
plat_needed = 1;
for (int j = i + 1; j < n; j++) {
// check for overlap
if ((arr[i] >= arr[j] && arr[i] <= dep[j]) ||
(arr[j] >= arr[i] && arr[j] <= dep[i]))
plat_needed++;
}
// update result
result = max(result, plat_needed);
}
return result;
}
// Driver Code
int main()
{
int arr[] = { 900, 940, 950, 1100, 1500, 1800 };
int dep[] = { 910, 1200, 1120, 1130, 1900, 2000 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Minimum Number of Platforms Required = "
<< findPlatform(arr, dep, n);
return 0;
}
Java
// Program to find minimum number of platforms
// required on a railway station
import java.io.*;
class GFG {
// Returns minimum number of platforms reqquired
public static int findPlatform(int arr[], int dep[],
int n)
{
// plat_needed indicates number of platforms
// needed at a time
int plat_needed = 1, result = 1;
int i = 1, j = 0;
// run a nested loop to find overlap
for (i = 0; i < n; i++) {
// minimum platform
plat_needed = 1;
for (j = i + 1; j < n; j++) {
// check for overlap
if ((arr[i] >= arr[j] && arr[i] <= dep[j])
|| (arr[j] >= arr[i]
&& arr[j] <= dep[i]))
plat_needed++;
}
// update result
result = Math.max(result, plat_needed);
}
return result;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 900, 940, 950, 1100, 1500, 1800 };
int dep[] = { 910, 1200, 1120, 1130, 1900, 2000 };
int n = 6;
System.out.println(
"Minimum Number of Platforms Required = "
+ findPlatform(arr, dep, n));
}
}
Javascript
C++
// Program to find minimum number of platforms
// required on a railway station
#include
#include
using namespace std;
// 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++;
}
// Else decrement count of platforms needed
else if (arr[i] > dep[j]) {
plat_needed--;
j++;
}
// Update result if needed
if (plat_needed > result)
result = plat_needed;
}
return result;
}
// Driver code
int main()
{
int arr[] = { 900, 940, 950, 1100, 1500, 1800 };
int dep[] = { 910, 1200, 1120, 1130, 1900, 2000 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Minimum Number of Platforms Required = "
<< findPlatform(arr, dep, n);
return 0;
}
Java
// Program to find minimum number of platforms
import java.util.*;
class GFG {
// Returns minimum number of platforms reqquired
static int findPlatform(int arr[], int dep[], int n)
{
// Sort arrival and departure arrays
Arrays.sort(arr);
Arrays.sort(dep);
// 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++;
}
// Else decrement count of platforms needed
else if (arr[i] > dep[j]) {
plat_needed--;
j++;
}
// Update result if needed
if (plat_needed > result)
result = plat_needed;
}
return result;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 900, 940, 950, 1100, 1500, 1800 };
int dep[] = { 910, 1200, 1120, 1130, 1900, 2000 };
int n = arr.length;
System.out.println("Minimum Number of Platforms Required = "
+ findPlatform(arr, dep, n));
}
}
Python3
# Program to find minimum
# number of platforms
# required on a railway
# station
# Returns minimum number
# of platforms reqquired
def findPlatform(arr, dep, n):
# Sort arrival and
# departure arrays
arr.sort()
dep.sort()
# plat_needed indicates
# number of platforms
# needed at a time
plat_needed = 1
result = 1
i = 1
j = 0
# Similar to merge in
# merge sort to process
# all events in sorted order
while (i < n and j < n):
# If next event in sorted
# order is arrival,
# increment count of
# platforms needed
if (arr[i] <= dep[j]):
plat_needed += 1
i += 1
# Else decrement count
# of platforms needed
elif (arr[i] > dep[j]):
plat_needed -= 1
j += 1
# Update result if needed
if (plat_needed > result):
result = plat_needed
return result
# Driver code
arr = [900, 940, 950, 1100, 1500, 1800]
dep = [910, 1200, 1120, 1130, 1900, 2000]
n = len(arr)
print("Minimum Number of Platforms Required = ",
findPlatform(arr, dep, n))
# This code is contributed
# by Anant Agarwal.
C#
// C# program to find minimum number
// of platforms
using System;
class GFG {
// Returns minimum number of platforms
// reqquired
static int findPlatform(int[] arr, int[] dep, int n)
{
// Sort arrival and departure arrays
Array.Sort(arr);
Array.Sort(dep);
// 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++;
}
// Else decrement count of
// platforms needed
else if (arr[i] > dep[j])
{
plat_needed--;
j++;
}
// Update result if needed
if (plat_needed > result)
result = plat_needed;
}
return result;
}
// Driver code
public static void Main()
{
int[] arr = { 900, 940, 950, 1100, 1500, 1800 };
int[] dep = { 910, 1200, 1120, 1130, 1900, 2000 };
int n = arr.Length;
Console.Write("Minimum Number of "
+ " Platforms Required = "
+ findPlatform(arr, dep, n));
}
}
// This code os contributed by nitin mittal.
PHP
$dep[$j])
{
$plat_needed--;
$j++;
}
// Update result if needed
if ($plat_needed > $result)
$result = $plat_needed;
}
return $result;
}
// Driver Code
$arr = array(900, 940, 950, 1100, 1500, 1800);
$dep = array(910, 1200, 1120, 1130, 1900, 2000);
$n = count($arr);
echo "Minimum Number of Platforms Required = ", findPlatform($arr, $dep, $n);
// This code is contributed by anuj_67.
?>
输出
Minimum Number of Platforms Required = 3
复杂度分析:
- 时间复杂度: O(n ^ 2)。
两个嵌套循环遍历数组,因此时间复杂度为O(n ^ 2)。 - 空间复杂度: O(1)。
由于不需要额外的空间。
高效的解决方案:
- 方法:想法是按排序顺序考虑所有事件。一旦事件按顺序排序,就可以随时跟踪火车的数量,以跟踪已经到达但尚未出发的火车。
例如,考虑上面的例子。
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}
All events are sorted by time.
Total platforms at any time can be obtained by
subtracting total departures from total arrivals
by that time.
Time Event Type Total Platforms Needed
at this Time
9:00 Arrival 1
9:10 Departure 0
9:40 Arrival 1
9:50 Arrival 2
11:00 Arrival 3
11:20 Departure 2
11:30 Departure 1
12:00 Departure 0
15:00 Arrival 1
18:00 Arrival 2
19:00 Departure 1
20:00 Departure 0
Minimum Platforms needed on railway station
= Maximum platforms needed at any time
= 3
注意:此方法假定火车在同一日期到达和离开。
算法:
- 排序火车的到达和离开时间。
- 创建两个指针i = 0和j = 0,并创建一个变量来存储ans和当前计数平台
- 在i
- 如果到达时间小于或等于离开,则需要一个以上的平台,因此增加计数,即plat ++并增加i
- 否则,如果到达时间大于出发时间,则需要少一个平台,因此请减少计数,即平台和增加j
- 更新ans,即ans = max(ans,plat)。
实现:这不会创建所有事件的单个排序列表,而是单独对arr []和dep []数组进行排序,然后使用合并排序的合并过程将它们作为单个排序数组一起处理。
C++
// Program to find minimum number of platforms
// required on a railway station
#include
#include
using namespace std;
// 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++;
}
// Else decrement count of platforms needed
else if (arr[i] > dep[j]) {
plat_needed--;
j++;
}
// Update result if needed
if (plat_needed > result)
result = plat_needed;
}
return result;
}
// Driver code
int main()
{
int arr[] = { 900, 940, 950, 1100, 1500, 1800 };
int dep[] = { 910, 1200, 1120, 1130, 1900, 2000 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Minimum Number of Platforms Required = "
<< findPlatform(arr, dep, n);
return 0;
}
Java
// Program to find minimum number of platforms
import java.util.*;
class GFG {
// Returns minimum number of platforms reqquired
static int findPlatform(int arr[], int dep[], int n)
{
// Sort arrival and departure arrays
Arrays.sort(arr);
Arrays.sort(dep);
// 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++;
}
// Else decrement count of platforms needed
else if (arr[i] > dep[j]) {
plat_needed--;
j++;
}
// Update result if needed
if (plat_needed > result)
result = plat_needed;
}
return result;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 900, 940, 950, 1100, 1500, 1800 };
int dep[] = { 910, 1200, 1120, 1130, 1900, 2000 };
int n = arr.length;
System.out.println("Minimum Number of Platforms Required = "
+ findPlatform(arr, dep, n));
}
}
Python3
# Program to find minimum
# number of platforms
# required on a railway
# station
# Returns minimum number
# of platforms reqquired
def findPlatform(arr, dep, n):
# Sort arrival and
# departure arrays
arr.sort()
dep.sort()
# plat_needed indicates
# number of platforms
# needed at a time
plat_needed = 1
result = 1
i = 1
j = 0
# Similar to merge in
# merge sort to process
# all events in sorted order
while (i < n and j < n):
# If next event in sorted
# order is arrival,
# increment count of
# platforms needed
if (arr[i] <= dep[j]):
plat_needed += 1
i += 1
# Else decrement count
# of platforms needed
elif (arr[i] > dep[j]):
plat_needed -= 1
j += 1
# Update result if needed
if (plat_needed > result):
result = plat_needed
return result
# Driver code
arr = [900, 940, 950, 1100, 1500, 1800]
dep = [910, 1200, 1120, 1130, 1900, 2000]
n = len(arr)
print("Minimum Number of Platforms Required = ",
findPlatform(arr, dep, n))
# This code is contributed
# by Anant Agarwal.
C#
// C# program to find minimum number
// of platforms
using System;
class GFG {
// Returns minimum number of platforms
// reqquired
static int findPlatform(int[] arr, int[] dep, int n)
{
// Sort arrival and departure arrays
Array.Sort(arr);
Array.Sort(dep);
// 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++;
}
// Else decrement count of
// platforms needed
else if (arr[i] > dep[j])
{
plat_needed--;
j++;
}
// Update result if needed
if (plat_needed > result)
result = plat_needed;
}
return result;
}
// Driver code
public static void Main()
{
int[] arr = { 900, 940, 950, 1100, 1500, 1800 };
int[] dep = { 910, 1200, 1120, 1130, 1900, 2000 };
int n = arr.Length;
Console.Write("Minimum Number of "
+ " Platforms Required = "
+ findPlatform(arr, dep, n));
}
}
// This code os contributed by nitin mittal.
的PHP
$dep[$j])
{
$plat_needed--;
$j++;
}
// Update result if needed
if ($plat_needed > $result)
$result = $plat_needed;
}
return $result;
}
// Driver Code
$arr = array(900, 940, 950, 1100, 1500, 1800);
$dep = array(910, 1200, 1120, 1130, 1900, 2000);
$n = count($arr);
echo "Minimum Number of Platforms Required = ", findPlatform($arr, $dep, $n);
// This code is contributed by anuj_67.
?>
输出
Minimum Number of Platforms Required = 3
复杂度分析:
- 时间复杂度: O(N * log N)。
在对O(N * log N)进行排序后,需要对两个数组进行一次遍历O(n),因此时间复杂度为O(N * log N)。 - 空间复杂度: O(1)。
由于不需要额外的空间。