📜  给定时间表的 n 个批次的 m 个事件的最小房间数

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

学校有n个学生团体。在学校的每一天,都有 m 个时段。学生团体在某个时间段内可能有空,也可能有空。给定 n 个二进制字符串,其中每个二进制字符串的长度为 m。在第i个字符串在第j个位置的字符是0,如果第i组是在第j个时隙自由和1,如果第i组是忙。
我们的任务是确定在一个学习日为所有小组上课所需的最少房间数。请注意,一个房间在一个时间段内最多可以举办一个团体课程。
例子:

使用的方法:这里我们遍历我们拥有的字符串的每个字符,并在遍历的同时维护字符串每个位置处 1 的数量,因此我们知道每个特定时间段的重合类的数量。然后我们只需要找到所有时间段中重合类的最大数量。

C++
// CPP program to find minimum number of rooms
// required
#include 
using namespace std;
 
// Returns minimum number of rooms required
// to perform classes of n groups in m slots
// with given schedule.
int findMinRooms(string slots[], int n, int m)
{
    // Store count of classes happening in
    // every slot.
    int counts[m] = { 0 };
    for (int i = 0; i < n; i++)    
        for (int j = 0; j < m; j++)        
            if (slots[i][j] == '1')
                counts[j]++;
     
    // Number of rooms required is equal to
    // maximum classes happening in a
    // particular slot.
    return *max_element(counts, counts+m);     
}
 
// Driver Code
int main()
{
    int n = 3, m = 7;
    string slots[n] = { "0101011",
                        "0011001",
                        "0110111" };
    cout << findMinRooms(slots, n, m);
    return 0;
}


Java
// java program to find the minimum number
// of rooms required
class GFG {
 
    // Returns minimum number of rooms required
    // to perform classes of n groups in m slots
    // with given schedule.
    static int findMinRooms(String slots[],
                                   int n, int m)
    {
         
        // Store number of class happening in
        //empty slot
        int counts[] = new int[m];
         
        //initialize all values to zero
        for (int i = 0; i < m; i++)
            counts[i] = 0;
         
        for (int i = 0; i < n; i++)    
            for (int j = 0; j < m; j++)        
                if (slots[i].charAt(j) == '1')
                    counts[j]++;
         
        // Number of rooms required is equal to
        // maximum classes happening in a
        // particular slot.
         
        int max = -1;
        // find the max element
        for (int i = 0; i < m; i++)
            if(max < counts[i])
                max = counts[i];
         
        return max;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int n = 3, m = 7;
        String slots[] = { "0101011",
                           "0011001",
                           "0110111" };
        System.out.println( findMinRooms(slots, n, m));
    }
}
 
// This code is contributed by Arnab Kundu.


Python3
# Python3 program to find minimum
# number of rooms required
 
# Returns minimum number of
# rooms required to perform
# classes of n groups in m
# slots with given schedule.
def findMinRooms(slots, n, m):
 
    # Store count of classes
    # happening in every slot.
    counts = [0] * m;
    for i in range(n):
        for j in range(m):
            if (slots[i][j] == '1'):
                counts[j] += 1;
     
    # Number of rooms required is
    # equal to maximum classes
    # happening in a particular slot.
    return max(counts);
 
# Driver Code
n = 3;
m = 7;
slots = ["0101011", "0011001", "0110111"];
print(findMinRooms(slots, n, m));
 
# This code is contributed by mits


C#
// C# program to find the minimum number
// of rooms required
using System;
class GFG {
  
    // Returns minimum number of rooms required
    // to perform classes of n groups in m slots
    // with given schedule.
    static int findMinRooms(string []slots,
                                   int n, int m)
    {
          
        // Store number of class happening in
        //empty slot
        int []counts = new int[m];
          
        //initialize all values to zero
        for (int i = 0; i < m; i++)
            counts[i] = 0;
          
        for (int i = 0; i < n; i++)    
            for (int j = 0; j < m; j++)        
                if (slots[i][j] == '1')
                    counts[j]++;
          
        // Number of rooms required is equal to
        // maximum classes happening in a
        // particular slot.
          
        int max = -1;
        // find the max element
        for (int i = 0; i < m; i++)
            if(max < counts[i])
                max = counts[i];
          
        return max;
    }
      
    // Driver Code
    public static void Main()
    {
        int n = 3, m = 7;
        String []slots = { "0101011",
                           "0011001",
                           "0110111" };
        Console.Write( findMinRooms(slots, n, m));
    }
}
  
// This code is contributed by nitin mittal


PHP


Javascript


输出:

3

时间复杂度: O(m * n)
辅助空间: O(m)

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