Minimum rooms for m events of n batches with given schedule

There are n student groups at the school. On each day in school, there are m time slots. A student group may or may not be free during a time slot. We are given n binary string where each binary string is of length m. A character at j-th position in i-th string is 0 if i-th group is free in j-th slot and 1 if i-th group is busy.

Our task is to determine the minimum number of rooms needed to hold classes for all groups on a single study day. Note that one room can hold at most one group class in a single time slot.

Examples:

Input : n = 2, m = 7, slots[] = {“0101010”, “1010101”}
Output : 1
Explanation : Both group can hold their classes in a single room as they have alternative classes.

Input : n = 3, m = 7, slots[] = {“0101011”, “0011001”, “0110111”}
Output : 3



Approach used: Here we traverse through each character of strings we have and while traversing maintaining a count of the number of 1’s at each position of the strings and hence we know the number of coinciding classes at each particular time slot. Then we just need to find the maximum number of coinciding classes amongst all time slots.

CPP

// CPP program to find minimum number of rooms
// required
#include <bits/stdc++.h>
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.

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
Output:

3

Time Complexity : O(m * n)
Auxiliary Space : O(m)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Improved By : andrew1234, nitin mittal




Practice Tags :

Recommended Posts:



1.3 Average Difficulty : 1.3/5.0
Based on 3 vote(s)






User Actions