-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer.py
54 lines (51 loc) · 1.99 KB
/
answer.py
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
def solution(genres, plays):
answer = []
class music:
def __init__(self,genres):
self.genres = genres
self.first_index = -1
self.second_index = -1
self.dict={}
def push(self,plays,i):
self.dict[i]=plays
if self.first_index == -1:
self.first_index = i
self.second_index = i
else:
if plays>self.dict[self.first_index]:
tmp = self.first_index
self.first_index = i
self.second_index = tmp
elif plays == self.dict[self.first_index]:
if i<self.first_index:
tmp= self.first_index
self.first_index = i
self.second_index = tmp
else:
self.second_index = i
elif self.dict[self.first_index]>plays:
if self.first_index==self.second_index:
self.second_index = i
elif plays>self.dict[self.second_index]:
self.second_index = i
elif self.dict[self.second_index] == plays:
if i<self.second_index:
self.second_index = i
genres_dict = {}
for i in range(len(plays)):
if genres[i] not in genres_dict.keys():
genres_dict[genres[i]] = plays[i]
else:
genres_dict[genres[i]]+=plays[i]
genres_dict = {k: v for k, v in sorted(genres_dict.items(), key=lambda item: item[1])[::-1]}
for k in genres_dict.keys():
mu = music(k)
for i in range(len(plays)):
if genres[i] == k:
mu.push(plays[i],i)
if mu.second_index == mu.first_index:
answer.append(mu.first_index)
else:
answer.append(mu.first_index)
answer.append(mu.second_index)
return answer