给定一个二维数组arr[][] ,每一行代表一对代表汽车进出停车场的时间,任务是计算可以同时停放的最大汽车数量。
例子:
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.
做法:思路是用Kadane的算法来解决这个问题。请按照以下步骤解决问题:
- 初始化成对向量以将进入或退出时间存储为一对的第一个元素,并将 true 存储为一对的第二个元素,如果存储的相应时间是进入时间。否则,存储为假。
- 按时间的非递减顺序对向量进行排序。
- 初始化两个变量,比如curMax ,以查找数组的所有真实连续段,以及maxFinal ,以跟踪所有真实段中最长的真实连续段。
- 迭代范围[0, 2*N – 1]:
- 如果有车进入,将curMax增加1。
- 除此以外:
- 如果curMax > maxFinal ,更新maxFinal = curMax 。
- 每当汽车退出时,将curMax减去1 。
- 打印maxFinal作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count maximum number
// of cars parked at the same
int maxCars(int arr[][2], int N)
{
// Stores info about
// entry and exit times
pair a[2 * N];
// Convert given array to new array
for (int i = 0; i < N; i++) {
a[2 * i] = { arr[i][0], true };
a[2 * i + 1] = { arr[i][1], false };
}
// Sort array in ascending
// order of time
sort(a, a + 2 * N);
// Stores current maximum
// at every iteration
int curMax = 0;
// Stores final maximum number
// of cars parked at any time
int maxFinal = 0;
// Traverse the array
for (int i = 0; i < 2 * N; ++i) {
// When car entered
if (a[i].second) {
curMax++;
}
// When car exits
else {
if (curMax > maxFinal) {
maxFinal = curMax;
}
curMax--;
}
}
// Print the answer
cout << maxFinal;
}
// Driver Code
int main()
{
// Given array
int arr[][2] = { { 1012, 1136 },
{ 1317, 1412 },
{ 1015, 1020 } };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
maxCars(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Pair class
static class pair
{
int first;
boolean second;
pair(int first, boolean second)
{
this.first = first;
this.second = second;
}
}
// Function to count maximum number
// of cars parked at the same
static void maxCars(int arr[][], int N)
{
// Stores info about
// entry and exit times
pair a[] = new pair[2 * N];
// Convert given array to new array
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);
}
// Sort array in ascending
// order of time
Arrays.sort(a, (p1, p2) -> p1.first - p2.first);
// Stores current maximum
// at every iteration
int curMax = 0;
// Stores final maximum number
// of cars parked at any time
int maxFinal = 0;
// Traverse the array
for(int i = 0; i < 2 * N; ++i)
{
// When car entered
if (a[i].second)
{
curMax++;
}
// When car exits
else
{
if (curMax > maxFinal)
{
maxFinal = curMax;
}
curMax--;
}
}
// Print the answer
System.out.println(maxFinal);
}
// Driver Code
public static void main(String[] args)
{
// Given array
int arr[][] = { { 1012, 1136 },
{ 1317, 1412 },
{ 1015, 1020 } };
// Size of the array
int N = arr.length;
// Function Call
maxCars(arr, N);
}
}
// This code is contributed by Kingash
Python3
# Python3 program for the above approach
# Function to count maximum number
# of cars parked at the same
def maxCars(arr, N):
# Stores info about
# entry and exit times
a = [[0,True] for i in range(2 * N)]
# Convert given array to new array
for i in range(N):
a[2 * i] = [arr[i][0], True]
a[2 * i + 1] = [arr[i][1], False]
# Sort array in ascending
# order of time
a = sorted(a)
# Stores current maximum
# at every iteration
curMax = 0
# Stores final maximum number
# of cars parked at any time
maxFinal = 0
# Traverse the array
for i in range(2*N):
# When car entered
if (a[i][1]):
curMax += 1
# When car exits
else:
if (curMax > maxFinal):
maxFinal = curMax
curMax -= 1
# Prthe answer
print (maxFinal)
# Driver Code
if __name__ == '__main__':
# Given array
arr= [ [ 1012, 1136 ],
[ 1317, 1412 ],
[ 1015, 1020 ]]
# Size of the array
N = len(arr)
# Function Call
maxCars(arr, N)
# This code is contributed by mohit kumar 29.
输出:
2
时间复杂度: O(N)
辅助空间: O(N)