📜  找到容纳乘客所需的火车的最小载客量

📅  最后修改于: 2021-04-29 05:53:08             🧑  作者: Mango

给定进出火车的乘客数量,任务是找到火车的最小容量,以在整个旅程中保持所有乘客在场。

例子:

方法:可以通过添加进入火车的人数并减去离开火车的人数,来计算特定站点上火车的当前容量。所需的最小容量将是所有站点上当前容量的所有值中的最大值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the minimum capacity required
int minCapacity(int enter[], int exit[], int n)
{
  
    // To store the minimum capacity
    int minCap = 0;
  
    // To store the current capacity
    // of the train
    int currCap = 0;
  
    // For every station
    for (int i = 0; i < n; i++) {
  
        // Add the number of people entering the
        // train and subtract the number of people
        // exiting the train to get the
        // current capacity of the train
        currCap = currCap + enter[i] - exit[i];
  
        // Update the minimum capacity
        minCap = max(minCap, currCap);
    }
  
    return minCap;
}
  
// Driver code
int main()
{
    int enter[] = { 3, 5, 2, 0 };
    int exit[] = { 0, 2, 4, 4 };
    int n = sizeof(enter) / sizeof(enter[0]);
  
    cout << minCapacity(enter, exit, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
// Function to return the minimum capacity required
static int minCapacity(int enter[], 
                       int exit[], int n)
{
  
    // To store the minimum capacity
    int minCap = 0;
  
    // To store the current capacity
    // of the train
    int currCap = 0;
  
    // For every station
    for (int i = 0; i < n; i++)
    {
  
        // Add the number of people entering the
        // train and subtract the number of people
        // exiting the train to get the
        // current capacity of the train
        currCap = currCap + enter[i] - exit[i];
  
        // Update the minimum capacity
        minCap = Math.max(minCap, currCap);
    }
    return minCap;
}
  
// Driver code
public static void main(String[] args) 
{
    int enter[] = { 3, 5, 2, 0 };
    int exit[] = { 0, 2, 4, 4 };
    int n = enter.length;
  
    System.out.println(minCapacity(enter, exit, n));
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
  
# Function to return the 
# minimum capacity required
def minCapacity(enter, exit, n):
      
    # To store the minimum capacity
    minCap = 0;
  
    # To store the current capacity
    # of the train
    currCap = 0;
  
    # For every station
    for i in range(n):
          
        # Add the number of people entering the
        # train and subtract the number of people
        # exiting the train to get the
        # current capacity of the train
        currCap = currCap + enter[i] - exit[i];
  
        # Update the minimum capacity
        minCap = max(minCap, currCap);
    return minCap;
  
# Driver code
if __name__ == '__main__':
    enter = [3, 5, 2, 0];
    exit = [0, 2, 4, 4];
    n = len(enter);
  
    print(minCapacity(enter, exit, n));
  
# This code is contributed by Princi Singh


C#
// C# implementation of the approach 
using System;
  
class GFG 
{
  
// Function to return the minimum 
// capacity required
static int minCapacity(int []enter, 
                       int []exit, int n)
{
  
    // To store the minimum capacity
    int minCap = 0;
  
    // To store the current capacity
    // of the train
    int currCap = 0;
  
    // For every station
    for (int i = 0; i < n; i++)
    {
  
        // Add the number of people entering the
        // train and subtract the number of people
        // exiting the train to get the
        // current capacity of the train
        currCap = currCap + enter[i] - exit[i];
  
        // Update the minimum capacity
        minCap = Math.Max(minCap, currCap);
    }
    return minCap;
}
  
// Driver code
public static void Main(String[] args) 
{
    int []enter = { 3, 5, 2, 0 };
    int []exit = { 0, 2, 4, 4 };
    int n = enter.Length;
  
    Console.WriteLine(minCapacity(enter, exit, n));
}
}
  
// This code is contributed by PrinciRaj1992


输出:
6

时间复杂度: O(n)