-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMaximum Compatibility Score Sum.cpp
64 lines (46 loc) · 2.21 KB
/
Maximum Compatibility Score Sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Solution by Rahul Surana
***********************************************************
There is a survey that consists of n questions where each question's answer is either 0 (no) or 1 (yes).
The survey was given to m students numbered from 0 to m - 1 and m mentors numbered from 0 to m - 1.
The answers of the students are represented by a 2D integer array students where students[i] is an integer array that
contains the answers of the ith student (0-indexed).
The answers of the mentors are represented by a 2D integer array mentors where mentors[j] is an integer array that contains
the answers of the jth mentor (0-indexed).
Each student will be assigned to one mentor, and each mentor will have one student assigned to them.
The compatibility score of a student-mentor pair is the number of answers that are the same for both the student and the mentor.
For example, if the student's answers were [1, 0, 1] and the mentor's answers were [0, 0, 1],
then their compatibility score is 2 because only the second and the third answers are the same.
You are tasked with finding the optimal student-mentor pairings to maximize the sum of the compatibility scores.
Given students and mentors, return the maximum compatibility score sum that can be achieved.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
int n,m;
vector<bool> v;
int score(vector<int> &s, vector<int> &t){
int a = 0;
for(int i = 0; i < n; i++) a+= s[i]==t[i];
return a;
}
int df(int i, vector<vector<int>>& students, vector<vector<int>>& mentors){
if(i>=m ) return 0;
int ans = 0;
for(int k = 0; k < m; k++){
if(!v[k]){
v[k] = true;
ans = max(ans, score(students[i], mentors[k]) + df(i+1,students,mentors));
v[k] = false;
}
}
return ans;
}
int maxCompatibilitySum(vector<vector<int>>& students, vector<vector<int>>& mentors) {
n = students[0].size();
m = students.size();
v.resize(m,false);
return df(0,students,mentors);
}
};