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++
// 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. |
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
<?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 ?> |
3
Time Complexity: O(m * n)
Auxiliary Space: O(m)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Schedule jobs so that each server gets equal load
- OYO Rooms Interview Experience | Set 6 (For Senior Software Developer)
- OYO Rooms Interview Experience (On-Campus)
- Minimum distance between the maximum and minimum element of a given Array
- Minimum given operations required to convert a given binary string to all 1's
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Minimum change in given value so that it lies in all given Ranges
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Minimum number of steps required to obtain the given Array by the given operations
- Minimum cost required to rearrange a given array to make it equal to another given array
- Count minimum character replacements required such that given string satisfies the given conditions
- Minimum sum by choosing minimum of pairs from array
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Remove minimum numbers from the array to get minimum OR value
- Maximum and minimum of an array using minimum number of comparisons
- Queries to increment array elements in a given range by a given value for a given number of times
- Find minimum time to finish all jobs with given constraints
- Transform One String to Another using Minimum Number of Given Operation
- Count minimum steps to get the given desired array
- Rearrange a given list such that it consists of alternating minimum maximum elements
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.

