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

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

给定2d数组arr [] [] ,每行代表一对,分别代表停车场中汽车的进出时间,任务是计算可同时停车的最大汽车数量。

例子:

方法:想法是使用Kadane的算法来解决此问题。请按照以下步骤解决问题:

  • 初始化成对向量,以将进入或退出时间存储为一对的第一个元素,将true存储为一对的第二个元素(如果存储的对应时间是进入时间)。否则,将其存储为false。
  • 以不递减的时间顺序对向量进行排序。
  • 初始化两个变量,例如curMax ,以查找数组的所有真实连续段,以及maxFinal ,以跟踪所有真实段中最长的真实连续段。
  • 迭代范围[0,2 * N – 1]:
    • 如果有汽车进入,则将curMax递增1。
    • 除此以外:
      • 如果curMax > maxFinal ,则更新maxFinal = curMax
      • 每当有汽车驶出时,将curMax1
  • 打印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)