给定到达火车站的所有火车的到达和离开时间,任务是找到火车站所需的最少站台数量,以便没有火车等待。
我们得到两个数组,分别代表停靠的列车的到达和出发时间。
例子:
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 到结束。
- 对于外循环的每次迭代,找到与当前间隔相交的间隔计数。
- 在外循环的每次迭代中使用最大重叠计数更新答案。
- 打印答案。
执行:
C++14
// Program to find minimum number of platforms
// required on a railway station
#include
#include
using namespace std;
// Returns minimum number of platforms required
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 required
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));
}
}
C#
// Program to find minimum number of platforms
// required on a railway station
using System;
public class GFG{
// Returns minimum number of platforms required
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
static public void Main (){
int[] arr = { 900, 940, 950, 1100, 1500, 1800 };
int[] dep = { 910, 1200, 1120, 1130, 1900, 2000 };
int n = 6;
Console.WriteLine(
"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 required
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 required
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 required
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
// required
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.
?>
Javascript
输出
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和当前计数平台
- 运行循环 while i
- 如果到达时间小于或等于出发时间,则需要再增加一个平台,因此增加计数,即 plat++ 并增加 i
- 否则,如果到达时间大于出发时间,则需要少一个平台,因此减少计数,即 plat– 并增加 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 required
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 required
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));
}
}
蟒蛇3
# Program to find minimum
# number of platforms
# required on a railway
# station
# Returns minimum number
# of platforms required
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
// required
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.
?>
Javascript
输出
Minimum Number of Platforms Required = 3
复杂度分析:
- 时间复杂度: O(N * log N)。
排序O(N * log N)后需要对两个数组进行一次遍历O(n),所以时间复杂度为O(N * log N)。 - 空间复杂度: O(1)。
因为不需要额外的空间。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。