-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathQuestion To Volunteer Mapping.java
82 lines (72 loc) · 3.61 KB
/
Question To Volunteer Mapping.java
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// https://leetcode.com/discuss/interview-question/4820505/Google-question/
public class Main {
private static boolean dfs(Map<String, Set<Integer>> volunteerNameToQuestionIdMap, String volunteerName, Set<Integer> visitedQuestionIds, Map<Integer, String> questionIdToVolunteerNameMatches, List<Question> questions) {
for (Question question: questions) {
// Check if it can be mapped to current volunteer name
if (volunteerNameToQuestionIdMap.get(volunteerName).contains(question.id) && !visitedQuestionIds.contains(question.id)) {
visitedQuestionIds.add(question.id);
// Check if the question was already mapped or if the user who took this question can be mapped to other question
if (!questionIdToVolunteerNameMatches.containsKey(question.id) || dfs(volunteerNameToQuestionIdMap, questionIdToVolunteerNameMatches.get(question.id), visitedQuestionIds, questionIdToVolunteerNameMatches, questions)) {
questionIdToVolunteerNameMatches.put(question.id, volunteerName);
return true;
}
}
}
return false;
}
public static void main(String[] args) {
List<Question> questions = new ArrayList<>();
questions.add(new Question(1, new String[]{"MAC", "VSCODE"}));
questions.add(new Question(2, new String[]{"PY", "AI"}));
questions.add(new Question(3, new String[]{"JAVA", "OS"}));
questions.add(new Question(4, new String[]{"PY", "NW"}));
List<Volunteer> volunteers = new ArrayList<>();
volunteers.add(new Volunteer(1, new String[]{"PY", "NW"}, "A"));
volunteers.add(new Volunteer(2, new String[]{"AI"}, "B"));
volunteers.add(new Volunteer(3, new String[]{"JAVA", "NW"}, "C"));
volunteers.add(new Volunteer(4, new String[]{"JAVA", "NW"}, "D"));
Map<String, List<Integer>> questionToQuestionIdMap = new HashMap<>();
for (Question q: questions) {
for (String tag: q.tags) {
if (!questionToQuestionIdMap.containsKey(tag)) {
questionToQuestionIdMap.put(tag, new ArrayList<>());
}
questionToQuestionIdMap.get(tag).add(q.id);
}
}
Map<String, Set<Integer>> volunteerNameToQuestionIdMap = new HashMap<>();
for (Volunteer v: volunteers) {
volunteerNameToQuestionIdMap.put(v.name, new HashSet<>());
for (String tag: v.tags) {
if (questionToQuestionIdMap.containsKey(tag)) {
for (int questionId: questionToQuestionIdMap.get(tag)) {
volunteerNameToQuestionIdMap.get(v.name).add(questionId);
}
}
}
}
Map<Integer, String> questionIdToVolunteerNameMatches = new HashMap<>();
for (String volunteerName: volunteerNameToQuestionIdMap.keySet()) {
dfs(volunteerNameToQuestionIdMap, volunteerName, new HashSet<>(), questionIdToVolunteerNameMatches, questions);
}
System.out.println(questionIdToVolunteerNameMatches);
}
}
class Question {
int id;
String[] tags;
Question(int id, String[] tags) {
this.id = id;
this.tags = tags;
}
}
class Volunteer {
int id;
String[] tags;
String name;
Volunteer(int id, String[] tags, String name) {
this.id = id;
this.tags = tags;
this.name = name;
}
}