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

📅  最后修改于: 2021-05-07 09:53:08             🧑  作者: 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];
          
        //initilize 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];
           
        //initilize 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


输出:
3

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