📜  计算同时停放的最大汽车数量

📅  最后修改于: 2021-09-04 08:38:07             🧑  作者: Mango

给定一个二维数组arr[][] ,每一行代表一对代表汽车进出停车场的时间,任务是计算可以同时停放的最大汽车数量。

例子:

做法:思路是用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)