Ninja and his friends are ready to grab internships at different startups. There are ‘N’ applicants and ‘M’ internships available. Each internship has a requirement of one person and each person can be appointed for a single internship.
Given ‘N’, ‘M’ and a matrix ‘G’ of size ‘N x M’ where ‘G(i,j)’ denotes whether ‘ith’ applicant is interested in ‘jth’ internship. Find an assignment of internships such that as many applicants as possible get internships.
Output the maximum number of applicants that can get an internship.
Example : N = 2
M = 3
G = [{ 1, 0, 1 }, { 0, 1, 1} ]
Explanation :
One of the possible ways is first applicant gets internship 1 and second applicant gets internship 3.
Another possible way can be that the first applicant gets internship 3 and second applicant gets internship 2.
Hence, both of them can get internships.
The first line contains an integer 'T' which denotes the number of test cases to be run. Then the test cases follow.
The first line of each test case contains two integers ‘N’ and ‘M’ denoting the dimensions of the given grid.
The next ‘N’ lines contain ‘M’ integers each. Each integer is guaranteed to be ‘0’ or ‘1’.
Output format :
For each test case, print a single integer denoting the maximum number of applicants that can be assigned an internship.
Print the output of each test case in a new line.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 100
1 <= M <= 100
G[i][j] = ‘0’ or G[i][j] = ‘1’
Time Limit : 1 sec
2
3 5
1 1 0 1 1
0 1 0 0 1
1 1 0 1 1
4 2
0 1
0 1
0 1
0 1
3
1
For test case 1 we have,
Applicant 1 can get internship 1, Applicant 2 can get internship 2 and applicant 3 can get internship 5.
Hence, we output 3.
For test case 2 we have,
All applicants are interested in only internship 2. So, only one of them can get an internship.
So, we output 1.
2
2 5
1 0 1 0 0
0 0 1 0 0
2 4
0 1 1 1
1 0 0 0
2
2
Try to recursively assign internships to all students.
Approach :
Algorithm :
O(M*(N^M)) , where ‘N’ is the number of applicants and ‘M’ is the number of internships.
We are recursively checking all assignments, so the time complexity is O(M*(N^M)).
O(M), where ‘N’ is the number of applicants and ‘M’ is the number of internships.
We are maintaining a vector of size ‘M’ to check available internships, so the overall complexity is O(M).