|
์ฝ๋ ์์ฑํ๊ธฐ
def solution(genres, plays):
answer = []
dic = {}
for i,(g,p) in enumerate(zip(genres, plays)):
if g in dic:
dic[g].append([p,i])
dic[g][0] += p
else:
dic[g] = [p, [p,i]]
dic = sorted(dic.items(), key=lambda item : item[1][0], reverse=True)
print(dic)
for d in dic:
tmp = sorted(d[1][1:], key=lambda x:(-x[0],x[1]))
if len(tmp) < 2:
answer.append(tmp[0][1])
else:
answer.extend([tmp[0][1], tmp[1][1]])
return answer
# print(solution(["classic", "pop", "classic", "classic", "pop"], [500, 600, 150, 800, 2500]))
print(solution(["classic", "pop", "classic", "classic", "pop", "test"], [500, 600, 150, 800, 2500, 100]))
# print(solution(["classic"], [300]))
Python
๋ณต์ฌ
|
์ฝ๋ ์ค๋ช
ํ๊ธฐ
1.
genres์ plays ๋ฐฐ์ด์ ๋์์ ๋๋ฉด์ dic์ โ์ฅ๋ฅดโ : [์ ์ฒด ์ฅ๋ฅด ์ฌ์๋ ํ์์ ํฉ, [์ฌ์ ํ์, ๊ณ ์ ๋ฒํธ]] ํํ๋ก ์ ์ฅํ๋ค.
2.
์ํ ๋
ธ๋๊ฐ ๊ฐ์ฅ ๋ง์ด ์ฌ์๋ ์ฅ๋ฅด๋ฅผ ๋จผ์ ์๋กํ๋ฏ๋ก ์ ์ฒด ์ฅ๋ฅด ์ฌ์ ํ์๋ฅผ ๊ธฐ์ค์ผ๋ก dic์ ์ ๋ ฌํ๋ค.
3.
์ ๋ ฌ๋ dic๋ฅผ ์ํํ๋ฉด์ ๊ฐ ์ฅ๋ฅด ๋ณ๋ก ์ ๋ ฌ์ ํ๋ค.
a.
์ฅ๋ฅด ๋ด์์๋ ๋ง์ด ์ฌ์๋ ๋
ธ๋ ์์ผ๋ก ๊ทธ๋ฆฌ๊ณ ์ฌ์ํ์๊ฐ ๊ฐ๋ค๋ฉด ๊ณ ์ ๋ฒํธ๊ฐ ๋ฎ์ ๋
ธ๋ ์์ผ๋ก ์ ๋ ฌํ๋ค.
b.
๋ง์ฝ ์ฅ๋ฅด ๋ด ๋
ธ๋์ ๊ฐ์๊ฐ 1๊ฐ๋ผ๋ฉด 1๊ฐ์ ๊ณก์ ์๋กํ๋ค.