Amazon | OA 2019 | Favorite Genres

link

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
// import java.util.*;
class Solution {
static Map<String, List<String>> favoritegenre(Map<String, List<String>> userMap,
Map<String, List<String>> genreMap) {

Map<String, List<String>> res = new HashMap<>(); // map<user, user's favor genre>
Map<String, String> songstogenre = new HashMap<>();

for (String genre : genreMap.keySet()) {
List<String> songs = genreMap.get(genre);
for (String song : songs) {
songstogenre.put(song, genre);
}
}

for (String user : userMap.keySet()) {
Map<String, Integer> count = new HashMap<>(); // map<genre, genre_count>
int max = 0;
res.put(user, new ArrayList<>());
List<String> songs = userMap.get(user);
for (String song : songs) {
String genre = songstogenre.get(song);
int c = count.getOrDefault(genre, 0) + 1;
count.put(genre, c);
max = Math.max(c, max);
}
for (String key : count.keySet()) {
if (key!=null&&count.get(key) == max) { // if genre exists and is the most frequent one
res.get(user).add(key);
}
}
}

return res;
}
}
/*
public class MyClass {
public static void main(String[] args) {
Map<String, List<String>> userMap1 = new HashMap<>();
userMap1.put("David", Arrays.asList("song1", "song2", "song3", "song4", "song8"));
userMap1.put("Emma", Arrays.asList("song5", "song6", "song7"));

Map<String, List<String>> songGenres1 = new HashMap<>();
songGenres1.put("Rock", Arrays.asList("song1", "song3"));
songGenres1.put("Dubstep", Arrays.asList("song7"));
songGenres1.put("Techno", Arrays.asList("song2", "song4"));
songGenres1.put("Pop", Arrays.asList("song5", "song6"));
songGenres1.put("Jazz", Arrays.asList("song8", "song9"));

System.out.println("Result of test1:\n"+Solution.favoritegenre(userMap1, songGenres1));

Map<String, List<String>> userMap2 = new HashMap<>();
userMap2.put("David", Arrays.asList("song1", "song2"));
userMap2.put("Emma", Arrays.asList("song3", "song4"));

Map<String, List<String>> songGenres2 = new HashMap<>();

System.out.println("Result of test2:\n"+Solution.favoritegenre(userMap2, songGenres2));
}
}*/




0%