📜  课程安排所需的最小大厅

📅  最后修改于: 2021-10-26 05:42:09             🧑  作者: Mango

给定N 个讲座时间,以及它们的开始时间和结束时间(两者都包括在内),任务是找到举办所有课程所需的最小大厅数,以便在给定时间一个大厅只能用于一场讲座。请注意,最大结束时间可以是 10 5
例子:

方法:

  • 假设时间 T 从 0 开始。任务是找到在特定时间实例正在进行的最大讲座数。这将提供安排所有讲座所需的最少大厅数量。
  • 查找任何时间正在进行的讲座数量。维护一个 prefix_sum[] ,它将存储在时间 t 的任何实例正在进行的讲座数量。对于任何时间在 [s, t] 之间的讲座,执行 prefix_sum[s]++ 和 prefix_sum[t + 1]–。
  • 之后,这个前缀数组的累积总和将给出在任何时间进行的讲座计数。
  • 阵列中任何时刻 t 的最大值是所需的最小大厅数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
#define MAX 100001
 
// Function to return the minimum
// number of halls required
int minHalls(int lectures[][2], int n)
{
 
    // Array to store the number of
    // lectures ongoing at time t
    int prefix_sum[MAX] = { 0 };
 
    // For every lecture increment start
    // point s decrement (end point + 1)
    for (int i = 0; i < n; i++) {
        prefix_sum[lectures[i][0]]++;
        prefix_sum[lectures[i][1] + 1]--;
    }
 
    int ans = prefix_sum[0];
 
    // Perform prefix sum and update
    // the ans to maximum
    for (int i = 1; i < MAX; i++) {
        prefix_sum[i] += prefix_sum[i - 1];
        ans = max(ans, prefix_sum[i]);
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int lectures[][2] = { { 0, 5 },
                          { 1, 2 },
                          { 1, 10 } };
    int n = sizeof(lectures) / sizeof(lectures[0]);
 
    cout << minHalls(lectures, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
static int MAX = 100001;
 
// Function to return the minimum
// number of halls required
static int minHalls(int lectures[][], int n)
{
 
    // Array to store the number of
    // lectures ongoing at time t
    int []prefix_sum = new int[MAX];
 
    // For every lecture increment start
    // point s decrement (end point + 1)
    for (int i = 0; i < n; i++)
    {
        prefix_sum[lectures[i][0]]++;
        prefix_sum[lectures[i][1] + 1]--;
    }
 
    int ans = prefix_sum[0];
 
    // Perform prefix sum and update
    // the ans to maximum
    for (int i = 1; i < MAX; i++)
    {
        prefix_sum[i] += prefix_sum[i - 1];
        ans = Math.max(ans, prefix_sum[i]);
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int lectures[][] = {{ 0, 5 },
                        { 1, 2 },
                        { 1, 10 }};
    int n = lectures.length;
 
    System.out.println(minHalls(lectures, n));
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
MAX = 100001
 
# Function to return the minimum
# number of halls required
def minHalls(lectures, n) :
 
    # Array to store the number of
    # lectures ongoing at time t
    prefix_sum = [0] * MAX;
     
    # For every lecture increment start
    # point s decrement (end point + 1)
    for i in range(n) :
        prefix_sum[lectures[i][0]] += 1;
        prefix_sum[lectures[i][1] + 1] -= 1;
         
    ans = prefix_sum[0];
     
    # Perform prefix sum and update
    # the ans to maximum
    for i in range(1, MAX) :
        prefix_sum[i] += prefix_sum[i - 1];
        ans = max(ans, prefix_sum[i]);
         
    return ans;
 
# Driver code
if __name__ == "__main__" :
 
    lectures = [[ 0, 5 ],
                [ 1, 2 ],
                [ 1, 10 ]];
                 
    n = len(lectures);
 
    print(minHalls(lectures, n));
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
     
class GFG
{
static int MAX = 100001;
 
// Function to return the minimum
// number of halls required
static int minHalls(int [,]lectures, int n)
{
 
    // Array to store the number of
    // lectures ongoing at time t
    int []prefix_sum = new int[MAX];
 
    // For every lecture increment start
    // point s decrement (end point + 1)
    for (int i = 0; i < n; i++)
    {
        prefix_sum[lectures[i,0]]++;
        prefix_sum[lectures[i,1] + 1]--;
    }
 
    int ans = prefix_sum[0];
 
    // Perform prefix sum and update
    // the ans to maximum
    for (int i = 1; i < MAX; i++)
    {
        prefix_sum[i] += prefix_sum[i - 1];
        ans = Math.Max(ans, prefix_sum[i]);
    }
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int [,]lectures = {{ 0, 5 },
                       { 1, 2 },
                       { 1, 10 }};
    int n = lectures.GetLength(0);
 
    Console.WriteLine(minHalls(lectures, n));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
3

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程