class Report {
@Nested
class ReportCases {
@Test
public void case1 () {
String[] id_list = new String[] {"muzi", "frodo", "apeach", "neo"};
String[] report = new String[]{"muzi frodo", "apeach frodo", "frodo neo", "muzi neo", "apeach muzi"};
int count = 2;
int[] expect = new int[] {2,1,1,0};
Assertions.assertArrayEquals(expect, solution(id_list, report, count));
}
@Test
public void case2 () {
String[] id_list = new String[] {"con", "ryan"};
String[] report = new String[]{"ryan con", "ryan con", "ryan con", "ryan con"};
int count = 3;
int[] expect = new int[] {0,0};
Assertions.assertArrayEquals(expect, solution(id_list, report, count));
}
}
public int[] solution( String [] id_list, String[] report, int k ) {
Map<String, Integer> reportMap = new HashMap<>();
Map<String, List<String>> iReported = new HashMap<>();
Set<String> status = new HashSet<>();
for( String element : report ) {
if( status.contains(element) ) continue;
status.add(element);
String[] split = element.split(" ");
String reporter = split[0];
String reportee = split[1];
iReported.putIfAbsent(reporter, new ArrayList<>());
iReported.computeIfPresent(reporter, (s, strings) -> {
strings.add(reportee);
return strings;
});
reportMap.putIfAbsent(reportee, 0);
reportMap.computeIfPresent(reportee, (s, integer) -> integer + 1);
}
int[] result = Arrays.stream(id_list).mapToInt(key -> {
List<String> r = iReported.getOrDefault(key, new ArrayList<>());
if( r.isEmpty() ) return 0;
else {
return (int) r.stream()
.map(keyIn -> reportMap.getOrDefault(keyIn, 0))
.filter(count -> count >= k).count();
}
}).toArray();
return result;
}
}