-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResult.java
More file actions
51 lines (44 loc) · 1.82 KB
/
Result.java
File metadata and controls
51 lines (44 loc) · 1.82 KB
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
package hackrank.algorithm.implement.team;
import java.util.Arrays;
import java.util.List;
/**
* @see <a href="https://www.hackerrank.com/challenges/acm-icpc-team">ACM ICPC Team</a>
*/
public class Result {
/**
* @param topicsKnowledge Each element is a binary string that represents a person's knowledge of available topics.
* @return List of size 2. First element: max known topics. Second element: teams that know this many topics.
*/
public static List<Integer> acmTeam(List<String> topicsKnowledge) {
int numberPeople = topicsKnowledge.size();
int numberTopics = topicsKnowledge.get(0).length();
int[] teamKnownTopicsCount = new int[numberTopics + 1];
for (int person1 = 0; person1 < numberPeople; person1++) {
for (int person2 = person1 + 1; person2 < numberPeople; person2++) {
String person1Topics = topicsKnowledge.get(person1);
String person2Topics = topicsKnowledge.get(person2);
int count = countTeamKnownTopics(person1Topics, person2Topics);
teamKnownTopicsCount[count]++;
}
}
int maxTopics = 0;
int maxTeams = 0;
for (int i = teamKnownTopicsCount.length - 1; i >= 0; i--) {
if (teamKnownTopicsCount[i] > 0) {
maxTopics = i;
maxTeams = teamKnownTopicsCount[i];
break;
}
}
return Arrays.asList(maxTopics, maxTeams);
}
private static int countTeamKnownTopics(String person1Topics, String person2Topics) {
int knownTopics = 0;
for (int i = 0; i < person1Topics.length(); i++) {
if (person1Topics.charAt(i) == '1' || person2Topics.charAt(i) == '1') {
knownTopics++;
}
}
return knownTopics;
}
}