๐Ÿ“ Coding Test/Programmers

[Python] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต level2 (๋ฒ ์ŠคํŠธ์•จ๋ฒ”)

ใ…… ใ…œ ใ…” ใ…‡ 2023. 1. 12. 05:13
728x90

1. ๋ฌธ์ œ ์„ค๋ช…

์ŠคํŠธ๋ฆฌ๋ฐ ์‚ฌ์ดํŠธ์—์„œ ์žฅ๋ฅด ๋ณ„๋กœ ๊ฐ€์žฅ ๋งŽ์ด ์žฌ์ƒ๋œ ๋…ธ๋ž˜๋ฅผ ๋‘ ๊ฐœ์”ฉ ๋ชจ์•„ ๋ฒ ์ŠคํŠธ ์•จ๋ฒ”์„ ์ถœ์‹œํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋…ธ๋ž˜๋Š” ๊ณ ์œ  ๋ฒˆํ˜ธ๋กœ ๊ตฌ๋ถ„ํ•˜๋ฉฐ, ๋…ธ๋ž˜๋ฅผ ์ˆ˜๋กํ•˜๋Š” ๊ธฐ์ค€์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ์†ํ•œ ๋…ธ๋ž˜๊ฐ€ ๋งŽ์ด ์žฌ์ƒ๋œ ์žฅ๋ฅด๋ฅผ ๋จผ์ € ์ˆ˜๋กํ•ฉ๋‹ˆ๋‹ค.
  2. ์žฅ๋ฅด ๋‚ด์—์„œ ๋งŽ์ด ์žฌ์ƒ๋œ ๋…ธ๋ž˜๋ฅผ ๋จผ์ € ์ˆ˜๋กํ•ฉ๋‹ˆ๋‹ค.
  3. ์žฅ๋ฅด ๋‚ด์—์„œ ์žฌ์ƒ ํšŸ์ˆ˜๊ฐ€ ๊ฐ™์€ ๋…ธ๋ž˜ ์ค‘์—์„œ๋Š” ๊ณ ์œ  ๋ฒˆํ˜ธ๊ฐ€ ๋‚ฎ์€ ๋…ธ๋ž˜๋ฅผ ๋จผ์ € ์ˆ˜๋กํ•ฉ๋‹ˆ๋‹ค.

๋…ธ๋ž˜์˜ ์žฅ๋ฅด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด genres์™€ ๋…ธ๋ž˜๋ณ„ ์žฌ์ƒ ํšŸ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด plays๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ๋ฒ ์ŠคํŠธ ์•จ๋ฒ”์— ๋“ค์–ด๊ฐˆ ๋…ธ๋ž˜์˜ ๊ณ ์œ  ๋ฒˆํ˜ธ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.

 

 

 

 

2. ์ œํ•œ์‚ฌํ•ญ

  • genres[i]๋Š” ๊ณ ์œ ๋ฒˆํ˜ธ๊ฐ€ i์ธ ๋…ธ๋ž˜์˜ ์žฅ๋ฅด์ž…๋‹ˆ๋‹ค.
  • plays[i]๋Š” ๊ณ ์œ ๋ฒˆํ˜ธ๊ฐ€ i์ธ ๋…ธ๋ž˜๊ฐ€ ์žฌ์ƒ๋œ ํšŸ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • genres์™€ plays์˜ ๊ธธ์ด๋Š” ๊ฐ™์œผ๋ฉฐ, ์ด๋Š” 1 ์ด์ƒ 10,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ์žฅ๋ฅด ์ข…๋ฅ˜๋Š” 100๊ฐœ ๋ฏธ๋งŒ์ž…๋‹ˆ๋‹ค.
  • ์žฅ๋ฅด์— ์†ํ•œ ๊ณก์ด ํ•˜๋‚˜๋ผ๋ฉด, ํ•˜๋‚˜์˜ ๊ณก๋งŒ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ์žฅ๋ฅด๋Š” ์žฌ์ƒ๋œ ํšŸ์ˆ˜๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

 

 

 

 

 

 

 

3. ๋‚ด ํ’€์ด

from collections import defaultdict
def solution(genres, plays):
    dictionary = defaultdict(int)
    music = [[idx,x,y] for (idx,x),y in zip(enumerate(genres), plays)]
    for x,y in zip(genres, plays):
        dictionary[x] += y
    music.sort(key=lambda x: (dictionary[x[1]],x[2]),reverse=True)
    
    cnt = 0
    answer = [music[0][0]]
    for idx,x in enumerate(music[1:]):
        cnt += 1
        if music[idx-1][1] != x[1]:
            cnt = 1
        if cnt < 2:
            answer.append(x[0])
        
    return answer

 

 

 

 

 

 

 

4. ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

def solution(genres, plays):
    answer = []
    d = {e:[] for e in set(genres)}
    for e in zip(genres, plays, range(len(plays))):
        d[e[0]].append([e[1] , e[2]])
    genreSort =sorted(list(d.keys()), key= lambda x: sum( map(lambda y: y[0],d[x])), reverse = True)
    for g in genreSort:
        temp = [e[1] for e in sorted(d[g],key= lambda x: (x[0], -x[1]), reverse = True)]
        answer += temp[:min(len(temp),2)]
    return answer

 

728x90