Count maximum number of cars parked at the same time
Given a 2d array arr[][] with each row representing a pair representing entry and exit time of a car in a parking lot, the task is to calculate the maximum number of cars that can be parked at the same time.
Examples:
Input: arr[][] = {{1012, 1136}, {1317, 1417}, {1015, 1020}}
Output: 2
Explanation:
1st car entered at 10:12 and exits at 11:36 and 3rd car entered at 10:15 and exits at 10:20.
Therefore, 1st and 3rd car are present at the same time.
Input: arr[][] = {{1120, 1159}, {1508, 1529}, {1508, 1527}, {1503, 1600}, {1458, 1629}, {1224, 1313}}
Output: 4
Explanation: 2nd, 3rd, 4th and 5th cars are present at the same time.
Approach: The idea is to use Kadane’s algorithm to solve this problem. Follow the steps below to solve the problem:
- Initialize a vector of pairs to store the entry or exit time as the first element of a pair and true as the second element of a pair, if corresponding time stored is entry time. Otherwise, store as false.
- Sort the vector in non-decreasing order of time.
- Initialize two variables, say curMax, to look for all true contiguous segments of the array, and maxFinal, to keep track of longest true contiguous segment among all true segments.
- Iterate over the range [0, 2*N – 1]:
- If a car entered, increment curMax by 1.
- Otherwise:
- If curMax > maxFinal, update maxFinal = curMax.
- Whenever a car exits, subtract curMax by 1.
- Print maxFinal as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxCars( int arr[][2], int N)
{
pair< int , bool > a[2 * N];
for ( int i = 0; i < N; i++) {
a[2 * i] = { arr[i][0], true };
a[2 * i + 1] = { arr[i][1], false };
}
sort(a, a + 2 * N);
int curMax = 0;
int maxFinal = 0;
for ( int i = 0; i < 2 * N; ++i) {
if (a[i].second) {
curMax++;
}
else {
if (curMax > maxFinal) {
maxFinal = curMax;
}
curMax--;
}
}
cout << maxFinal;
}
int main()
{
int arr[][2] = { { 1012, 1136 },
{ 1317, 1412 },
{ 1015, 1020 } };
int N = sizeof (arr) / sizeof (arr[0]);
maxCars(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static class pair
{
int first;
boolean second;
pair( int first, boolean second)
{
this .first = first;
this .second = second;
}
}
static void maxCars( int arr[][], int N)
{
pair a[] = new pair[ 2 * N];
for ( int i = 0 ; i < N; i++)
{
a[ 2 * i] = new pair(arr[i][ 0 ], true );
a[ 2 * i + 1 ] = new pair(arr[i][ 1 ], false );
}
Arrays.sort(a, (p1, p2) -> p1.first - p2.first);
int curMax = 0 ;
int maxFinal = 0 ;
for ( int i = 0 ; i < 2 * N; ++i)
{
if (a[i].second)
{
curMax++;
}
else
{
if (curMax > maxFinal)
{
maxFinal = curMax;
}
curMax--;
}
}
System.out.println(maxFinal);
}
public static void main(String[] args)
{
int arr[][] = { { 1012 , 1136 },
{ 1317 , 1412 },
{ 1015 , 1020 } };
int N = arr.length;
maxCars(arr, N);
}
}
|
Python3
def maxCars(arr, N):
a = [[ 0 , True ] for i in range ( 2 * N)]
for i in range (N):
a[ 2 * i] = [arr[i][ 0 ], True ]
a[ 2 * i + 1 ] = [arr[i][ 1 ], False ]
a = sorted (a)
curMax = 0
maxFinal = 0
for i in range ( 2 * N):
if (a[i][ 1 ]):
curMax + = 1
else :
if (curMax > maxFinal):
maxFinal = curMax
curMax - = 1
print (maxFinal)
if __name__ = = '__main__' :
arr = [ [ 1012 , 1136 ],
[ 1317 , 1412 ],
[ 1015 , 1020 ]]
N = len (arr)
maxCars(arr, N)
|
C#
using System;
class GFG
{
static void maxCars( int [,] arr, int N)
{
Tuple< int , bool >[] a = new Tuple< int , bool >[2 * N];
for ( int i = 0; i < N; i++) {
a[2 * i] = new Tuple< int , bool >(arr[i,0], true );
a[2 * i + 1] = new Tuple< int , bool >(arr[i,1], false );
}
int curMax = 1;
int maxFinal = 0;
for ( int i = 0; i < 2 * N; ++i) {
if (a[i].Item2) {
curMax++;
}
else {
if (curMax > maxFinal) {
maxFinal = curMax;
}
curMax--;
}
}
Console.WriteLine(maxFinal);
}
static void Main ()
{
int [,] arr = { { 1012, 1136 },
{ 1317, 1412 },
{ 1015, 1020 } };
int N = 2;
maxCars(arr, N);
}
}
|
Javascript
<script>
class pair
{
constructor(first,second)
{
this .first = first;
this .second = second;
}
}
function maxCars(arr,N)
{
let a = new Array(2 * N);
for (let i = 0; i < N; i++)
{
a[2 * i] = new pair(arr[i][0], true );
a[2 * i + 1] = new pair(arr[i][1], false );
}
a.sort( function (p1, p2){ return p1.first - p2.first});
let curMax = 0;
let maxFinal = 0;
for (let i = 0; i < 2 * N; ++i)
{
if (a[i].second)
{
curMax++;
}
else
{
if (curMax > maxFinal)
{
maxFinal = curMax;
}
curMax--;
}
}
document.write(maxFinal+ "<br>" );
}
let arr=[[ 1012, 1136 ],
[ 1317, 1412 ],
[ 1015, 1020 ]];
let N = arr.length;
maxCars(arr, N);
</script>
|
Time Complexity: O(NLogN)
Auxiliary Space: O(N)
Last Updated :
22 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...