GFG App
Open App
Browser
Continue

# 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 :
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.

Implementation:

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

 ``

Output

`3`

Complexity Analysis:

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

My Personal Notes arrow_drop_up