[ 2023-04-20 ๐ŸŽง TIL ]

Burkeyยท2023๋…„ 4์›” 20์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
74/157

LV. 2 ๊ทค ๊ณ ๋ฅด๊ธฐ


๋ฌธ์ œ๋งํฌ

def solution(k, tangerine):
    tan_set = set(tangerine) 
	# ๊ทค ๋ฌด๊ฒŒ์˜ ์ข…๋ฅ˜ ๊ตฌํ•˜๋ ค๊ณ  ์ค‘๋ณต ์ œ๊ฑฐ
    tan_dict = {}
    count_k = i = answer = 0
    
    # ๊ทค์˜ ๋ฌด๊ฒŒ๋ฅผ key, ๊ทธ ๋ฌด๊ฒŒ์˜ ๊ฐฏ์ˆ˜๋ฅผ value๋กœ ๊ฐ–๋Š” dictionary ์ƒ์„ฑ
    for t in tangerine:
        if t in tan_dict:
            tan_dict[t] += 1
        else:
            tan_dict[t] = 1
            
    tan_dict = sorted(tan_dict.items(), key = lambda item: item[1], reverse = True)
    # ๊ทค์˜ ๊ฐฏ์ˆ˜ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ sort ์‹œํ‚ด
    
    while True:
        count_k += tan_dict[i][1]
        answer += 1

        if count_k >= k :
        # k์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ •๋ ฌ ํ–ˆ์œผ๋‹ˆ๊น ๋”ํ•ด์„œ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค.
            break
        else :
            i +=1
    return answer
profile
์Šคํƒฏ ์˜ฌ๋ฆฌ๋Š” ์ค‘

0๊ฐœ์˜ ๋Œ“๊ธ€