Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c77db82

Browse files
added Favorite Genres
1 parent 6d5df0c commit c77db82

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

‎Others/FavoriteGenres/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Favorite Genres
2+
3+
[Leetcode Link](https://leetcode.com/discuss/interview-question/373006)
4+
5+
## Problem:
6+
7+
Given a map `Map<String, List<String>>` userSongs with user names as keys and a list of all the songs that the user has listened to as values.
8+
9+
Also given a map `Map<String, List<String>>` songGenres, with song genre as keys and a list of all the songs within that genre as values. The song can only belong to only one genre.
10+
11+
The task is to return a map `Map<String, List<String>>`, where the key is a user name and the value is a list of the user's favorite genre(s). Favorite genre is the most listened to genre. A user can have more than one favorite genre if he/she has listened to the same number of songs per each of the genres.
12+
13+
## Example:
14+
15+
```
16+
Input:
17+
userSongs = {
18+
"David": ["song1", "song2", "song3", "song4", "song8"],
19+
"Emma": ["song5", "song6", "song7"]
20+
},
21+
songGenres = {
22+
"Rock": ["song1", "song3"],
23+
"Dubstep": ["song7"],
24+
"Techno": ["song2", "song4"],
25+
"Pop": ["song5", "song6"],
26+
"Jazz": ["song8", "song9"]
27+
}
28+
29+
Output: {
30+
"David": ["Rock", "Techno"],
31+
"Emma": ["Pop"]
32+
}
33+
34+
Explanation:
35+
David has 2 Rock, 2 Techno and 1 Jazz song. So he has 2 favorite genres.
36+
Emma has 2 Pop and 1 Dubstep song. Pop is Emma's favorite genre.
37+
```
38+
39+
```
40+
Input:
41+
userSongs = {
42+
"David": ["song1", "song2"],
43+
"Emma": ["song3", "song4"]
44+
},
45+
songGenres = {}
46+
47+
Output: {
48+
"David": [],
49+
"Emma": []
50+
}
51+
```

‎Others/FavoriteGenres/solution.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from collections import defaultdict, Counter
2+
3+
4+
def favoriteGenres(userSongs, songGenres):
5+
result = defaultdict(list)
6+
songGenreMap = dict()
7+
# create a map 1-to-1 map from song to genre
8+
for genre, songs in songGenres.items():
9+
for song in songs:
10+
songGenreMap[song] = genre
11+
# print(songGenreMap)
12+
# init result dict with names and genres
13+
for user, songs in userSongs.items():
14+
for song in songs:
15+
if song in songGenreMap:
16+
result[user].append(songGenreMap[song])
17+
# print(result)
18+
# keep the most genres
19+
for user, genres in result.items():
20+
counter = Counter(genres)
21+
maxCount = max(counter.values())
22+
result[user] = [genre for genre in counter if counter[genre] == maxCount]
23+
# print(result)
24+
return result
25+
26+
27+
userSongs = {
28+
"David": ["song1", "song2", "song3", "song4", "song8"],
29+
"Emma": ["song5", "song6", "song7"]
30+
}
31+
songGenres = {
32+
"Rock": ["song1", "song3"],
33+
"Dubstep": ["song7"],
34+
"Techno": ["song2", "song4"],
35+
"Pop": ["song5", "song6"],
36+
"Jazz": ["song8", "song9"]
37+
}
38+
print(favoriteGenres(userSongs, songGenres))
39+
userSongs = {
40+
"David": ["song1", "song2"],
41+
"Emma": ["song3", "song4"]
42+
}
43+
songGenres = {}
44+
45+
print(favoriteGenres(userSongs, songGenres))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /