铁路/汽车站所需的最低月台数量 | Set 2(基于集合的方法)
给定到达火车站的所有火车的到达和离开时间,找出火车站所需的最小月台数量,这样就没有火车等待。我们有两个数组,它们代表停靠的火车的到达和出发时间。
例子:
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)
我们已经在下面的帖子中讨论了它的简单和基于排序的解决方案。
铁路/汽车站所需的最少站台数量。
在这篇文章中,我们将所有到达和离开时间插入到一个多集中。 multiset 中 element 的第一个值表示到达/离开时间,第二个值表示到达或离开分别由 'a' 或 'd' 表示。
如果它到达,则增加 1,否则将值减少 1。如果我们从 STDIN 获取输入,那么我们可以直接将时间插入多重集中,而无需将时间存储在数组中。
C++
// C++ program to find minimum number of platforms
// required on a railway station
#include
using namespace std;
int findPlatform(int arr[], int dep[], int n)
{
// Insert all the times (arr. and dep.)
// in the multiset.
multiset > order;
for (int i = 0; i < n; i++) {
// If its arrival then second value
// of pair is 'a' else 'd'
order.insert(make_pair(arr[i], 'a'));
order.insert(make_pair(dep[i], 'd'));
}
int result = 0;
int plat_needed = 0;
// Start iterating the multiset.
for (auto it : order) {
// If its 'a' then add 1 to plat_needed
// else minus 1 from plat_needed.
if (it.second == 'a')
plat_needed++;
else
plat_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
// Java program to find minimum number
// of platforms required on a railway station
import java.io.*;
import java.util.*;
class pair
{
int first;
char second;
pair(int key1, char key2)
{
this.first = key1;
this.second = key2;
}
}
class GFG{
public static int findPlatform(int arr[], int dep[],
int n)
{
// Insert all the times (arr. and dep.)
// in the ArrayList of pairs.
ArrayList order = new ArrayList<>();
for(int i = 0; i < n; i++)
{
order.add(new pair(arr[i], 'a'));
order.add(new pair(dep[i], 'd'));
}
// Custom sort order ArrayList, first
// by time than by arrival or departure
Collections.sort(order, new Comparator()
{
public int compare(pair p1, pair p2)
{
if (p1.first == p2.first)
return new Character(p1.second)
.compareTo(
new Character(p2.second));
return p1.first - p2.first;
}
});
int result = 1;
int plat_needed = 0;
for(int i = 0; i < order.size(); i++)
{
pair p = order.get(i);
// If its 'a' then add 1 to plat_needed
// else minus 1 from plat_needed.
if (p.second == 'a')
plat_needed++;
else
plat_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 = 6;
System.out.println("Minimum Number of " +
"Platforms Required = " +
findPlatform(arr, dep, n));
}
}
// This code is contributed by RohitOberoi
Python3
# Python3 program to find minimum number
# of platforms required on a railway station
def findPlatform(arr, dep, n):
# Inserting all the arr. and dep. times
# in the array times
times = []
for i in range(n):
times.append([dep[i], 'd'])
times.append([arr[i], 'a'])
# Sort the array
times = sorted(times, key = lambda x: x[1])
times = sorted(times, key = lambda x: x[0])
result, plat_needed = 0, 0
for i in range(2 * n):
# If its 'a' then add 1 to plat_needed
# else minus 1 from plat_needed.
if times[i][1] == 'a':
plat_needed += 1
result = max(plat_needed, result)
else:
plat_needed -= 1
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 Tharun Reddy
C#
// C# program to find minimum number
// of platforms required on a railway station
using System;
using System.Collections.Generic;
public class pair {
public int first;
public char second;
public pair(int key1, char key2) {
this.first = key1;
this.second = key2;
}
}
public class GFG {
public static int findPlatform(int []arr, int []dep, int n) {
// Insert all the times (arr. and dep.)
// in the List of pairs.
List order = new List();
for (int i = 0; i < n; i++) {
order.Add(new pair(arr[i], 'a'));
order.Add(new pair(dep[i], 'd'));
}
// Custom sort order List, first
// by time than by arrival or departure
order.Sort((p1,p2)=> p1.first == p2.first? p1.second -
p2.second: p1.first - p2.first);
int result = 1;
int plat_needed = 0;
for (int i = 0; i < order.Count; i++) {
pair p = order[i];
// If its 'a' then add 1 to plat_needed
// else minus 1 from plat_needed.
if (p.second == 'a')
plat_needed++;
else
plat_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 = 6;
Console.WriteLine("Minimum Number of " +
"Platforms Required = " +
findPlatform(arr, dep, n));
}
}
// This code is contributed by Rajput-Ji
C++
// C++ program to find minimum number of platforms
// required on a railway station
#include
using namespace std;
int minPlatform(int arrival[], int departure[], int n)
{
// as time range from 0 to 2359 in 24 hour clock,
// we declare an array for values from 0 to 2360
int platform[2361] = {0};
int requiredPlatform = 1;
for (int i = 0; i < n; i++) {
// increment the platforms for arrival
++platform[arrival[i]];
// once train departs we decrease the platform count
--platform[departure[i] + 1];
}
// We are running loop till 2361 because maximum time value
// in a day can be 23:60
for (int i = 1; i < 2361; i++) {
// taking cumulative sum of platform give us required
// number of platform fro every minute
platform[i] = platform[i] + platform[i - 1];
requiredPlatform = max(requiredPlatform, platform[i]);
}
return requiredPlatform;
}
// 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 = "
<< minPlatform(arr, dep, n);
return 0;
}
Java
// Java program to find minimum number
// of platforms required on a railway
// station
import java.util.*;
import java.lang.*;
class GFG{
static int minPlatform(int arrival[],
int departure[],
int n)
{
// As time range from 0 to 2359 in
// 24 hour clock, we declare an array
// for values from 0 to 2360
int[] platform = new int[2361];
int requiredPlatform = 1;
for(int i = 0; i < n; i++)
{
// Increment the platforms for arrival
++platform[arrival[i]];
// Once train departs we decrease
// the platform count
--platform[departure[i] + 1];
}
// We are running loop till 2361 because
// maximum time value in a day can be 23:60
for(int i = 1; i < 2361; i++)
{
// Taking cumulative sum of platform
// give us required number of
// platform fro every minute
platform[i] = platform[i] +
platform[i - 1];
requiredPlatform = Math.max(requiredPlatform,
platform[i]);
}
return requiredPlatform;
}
// 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 = " +
minPlatform(arr, dep, n));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to find minimum number
# of platforms required on a railway station
def minPlatform(arrival, departure, n):
# As time range from 0 to 2359 in
# 24 hour clock, we declare an array
# for values from 0 to 2360
platform = [0] * 2631
requiredPlatform = 1
for i in range(n):
# Increment the platforms for arrival
platform[arrival[i]] += 1
# Once train departs we decrease the
# platform count
platform[departure[i] + 1] -= 1
# We are running loop till 2361 because
# maximum time value in a day can be 23:60
for i in range(1, 2631):
# Taking cumulative sum of
# platform give us required
# number of platform fro every minute
platform[i] = platform[i] + platform[i - 1]
requiredPlatform = max(requiredPlatform,
platform[i])
return requiredPlatform
# 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 = ",
minPlatform(arr, dep, n))
# This code is contributed by PawanJain1
C#
// C# program to find minimum number
// of platforms required on a railway
// station
using System;
class GFG {
static int minPlatform(int[] arrival, int[] departure, int n)
{
// As time range from 0 to 2359 in
// 24 hour clock, we declare an array
// for values from 0 to 2360
int[] platform = new int[2361];
int requiredPlatform = 1;
for(int i = 0; i < n; i++)
{
// Increment the platforms for arrival
++platform[arrival[i]];
// Once train departs we decrease
// the platform count
--platform[departure[i] + 1];
}
// We are running loop till 2361 because
// maximum time value in a day can be 23:60
for(int i = 1; i < 2361; i++)
{
// Taking cumulative sum of platform
// give us required number of
// platform fro every minute
platform[i] = platform[i] +
platform[i - 1];
requiredPlatform = Math.Max(requiredPlatform,
platform[i]);
}
return requiredPlatform;
}
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 = " +
minPlatform(arr, dep, n));
}
}
// This code is contributed by divyesh072019.
Javascript
输出:
Minimum Number of Platforms Required = 3
复杂性分析:
- 时间复杂度: O(N* LogN)。
因为我们要插入多重集,它会按排序顺序维护元素。因此,多重集中的 N 次插入操作需要 N*LogN 时间复杂度。
- 空间复杂度: O(N)。
我们正在使用具有 2*N 个元素的多重集。
C++
// C++ program to find minimum number of platforms
// required on a railway station
#include
using namespace std;
int minPlatform(int arrival[], int departure[], int n)
{
// as time range from 0 to 2359 in 24 hour clock,
// we declare an array for values from 0 to 2360
int platform[2361] = {0};
int requiredPlatform = 1;
for (int i = 0; i < n; i++) {
// increment the platforms for arrival
++platform[arrival[i]];
// once train departs we decrease the platform count
--platform[departure[i] + 1];
}
// We are running loop till 2361 because maximum time value
// in a day can be 23:60
for (int i = 1; i < 2361; i++) {
// taking cumulative sum of platform give us required
// number of platform fro every minute
platform[i] = platform[i] + platform[i - 1];
requiredPlatform = max(requiredPlatform, platform[i]);
}
return requiredPlatform;
}
// 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 = "
<< minPlatform(arr, dep, n);
return 0;
}
Java
// Java program to find minimum number
// of platforms required on a railway
// station
import java.util.*;
import java.lang.*;
class GFG{
static int minPlatform(int arrival[],
int departure[],
int n)
{
// As time range from 0 to 2359 in
// 24 hour clock, we declare an array
// for values from 0 to 2360
int[] platform = new int[2361];
int requiredPlatform = 1;
for(int i = 0; i < n; i++)
{
// Increment the platforms for arrival
++platform[arrival[i]];
// Once train departs we decrease
// the platform count
--platform[departure[i] + 1];
}
// We are running loop till 2361 because
// maximum time value in a day can be 23:60
for(int i = 1; i < 2361; i++)
{
// Taking cumulative sum of platform
// give us required number of
// platform fro every minute
platform[i] = platform[i] +
platform[i - 1];
requiredPlatform = Math.max(requiredPlatform,
platform[i]);
}
return requiredPlatform;
}
// 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 = " +
minPlatform(arr, dep, n));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to find minimum number
# of platforms required on a railway station
def minPlatform(arrival, departure, n):
# As time range from 0 to 2359 in
# 24 hour clock, we declare an array
# for values from 0 to 2360
platform = [0] * 2631
requiredPlatform = 1
for i in range(n):
# Increment the platforms for arrival
platform[arrival[i]] += 1
# Once train departs we decrease the
# platform count
platform[departure[i] + 1] -= 1
# We are running loop till 2361 because
# maximum time value in a day can be 23:60
for i in range(1, 2631):
# Taking cumulative sum of
# platform give us required
# number of platform fro every minute
platform[i] = platform[i] + platform[i - 1]
requiredPlatform = max(requiredPlatform,
platform[i])
return requiredPlatform
# 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 = ",
minPlatform(arr, dep, n))
# This code is contributed by PawanJain1
C#
// C# program to find minimum number
// of platforms required on a railway
// station
using System;
class GFG {
static int minPlatform(int[] arrival, int[] departure, int n)
{
// As time range from 0 to 2359 in
// 24 hour clock, we declare an array
// for values from 0 to 2360
int[] platform = new int[2361];
int requiredPlatform = 1;
for(int i = 0; i < n; i++)
{
// Increment the platforms for arrival
++platform[arrival[i]];
// Once train departs we decrease
// the platform count
--platform[departure[i] + 1];
}
// We are running loop till 2361 because
// maximum time value in a day can be 23:60
for(int i = 1; i < 2361; i++)
{
// Taking cumulative sum of platform
// give us required number of
// platform fro every minute
platform[i] = platform[i] +
platform[i - 1];
requiredPlatform = Math.Max(requiredPlatform,
platform[i]);
}
return requiredPlatform;
}
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 = " +
minPlatform(arr, dep, n));
}
}
// This code is contributed by divyesh072019.
Javascript
输出:
Minimum Number of Platforms Required = 3
复杂性分析:
- 时间复杂度: O(N)。
- 空间复杂度: O(1)。