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.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP 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.
function findMinRooms($slots, $n, $m)
{
    // Store count of classes 
    // happening in every slot.
    $counts = array_fill(0, $m, 0);
    for ($i = 0; $i < $n; $i++) 
        for ($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($counts);     
}
  
// Driver Code
$n = 3;
$m = 7;
$slots = array("0101011", "0011001"
                          "0110111");
echo findMinRooms($slots, $n, $m);
  
// This code is contributed by mits 
?>

chevron_right


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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.