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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinRooms(string slots[], int n, int m)
{
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]++;
return *max_element(counts, counts+m);
}
int main()
{
int n = 3, m = 7;
string slots[n] = { "0101011" ,
"0011001" ,
"0110111" };
cout << findMinRooms(slots, n, m);
return 0;
}
|
Java
class GFG {
static int findMinRooms(String slots[],
int n, int m)
{
int counts[] = new int [m];
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]++;
int max = - 1 ;
for ( int i = 0 ; i < m; i++)
if (max < counts[i])
max = counts[i];
return max;
}
public static void main(String args[])
{
int n = 3 , m = 7 ;
String slots[] = { "0101011" ,
"0011001" ,
"0110111" };
System.out.println( findMinRooms(slots, n, m));
}
}
|
Python3
def findMinRooms(slots, n, m):
counts = [ 0 ] * m;
for i in range (n):
for j in range (m):
if (slots[i][j] = = '1' ):
counts[j] + = 1 ;
return max (counts);
n = 3 ;
m = 7 ;
slots = [ "0101011" , "0011001" , "0110111" ];
print (findMinRooms(slots, n, m));
|
C#
using System;
class GFG {
static int findMinRooms( string []slots,
int n, int m)
{
int []counts = new int [m];
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]++;
int max = -1;
for ( int i = 0; i < m; i++)
if (max < counts[i])
max = counts[i];
return max;
}
public static void Main()
{
int n = 3, m = 7;
String []slots = { "0101011" ,
"0011001" ,
"0110111" };
Console.Write( findMinRooms(slots, n, m));
}
}
|
PHP
<?php
function findMinRooms( $slots , $n , $m )
{
$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 ]++;
return max( $counts );
}
$n = 3;
$m = 7;
$slots = array ( "0101011" , "0011001" ,
"0110111" );
echo findMinRooms( $slots , $n , $m );
?>
|
Javascript
<script>
function findMinRooms(slots, n, m)
{
let counts = Array(m).fill(0);
for (let i = 0; i < m; i++)
counts[i] = 0;
for (let i = 0; i < n; i++)
for (let j = 0; j < m; j++)
if (slots[i][j] == '1' )
counts[j]++;
let max = -1;
for (let i = 0; i < m; i++)
if (max < counts[i])
max = counts[i];
return max;
}
let n = 3, m = 7;
let slots = [ "0101011" ,
"0011001" ,
"0110111" ];
document.write( findMinRooms(slots, n, m));
</script>
|
Complexity Analysis:
- Time Complexity: O(m * n)
- Auxiliary Space: O(m)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Aug, 2022
Like Article
Save Article