๐Ÿ’ป Coding/[Algorithm]Python

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค | ์„ฑ๊ฒฉ ์œ ํ˜• ๊ฒ€์‚ฌํ•˜๊ธฐ โญ - Python

๐Ÿฉท ๋ฏผ์˜ 2024. 4. 2. 23:06

๐Ÿ—’๏ธ ๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/118666

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

์ฒ˜์Œ ๋ฌธ์ œ ์ ‘๊ทผํ•  ๋•Œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ๊ฐํ•˜์ง€๋ชปํ•˜๊ณ  result_list ์™€ cnt_list ๋ฅผ ๋”ฐ๋กœ ์ƒ์„ฑํ•˜์—ฌ ์ ‘๊ทผ์„ ํ•˜์˜€๋‹ค. 

๊ทธ๋Ÿฌ๋‚˜ ๋ฆฌ์ŠคํŠธ๋กœ ์ ‘๊ทผ์„ ํ•˜๋‹ˆ๊นŒ ํ•˜๋“œ์ฝ”๋”ฉ,,,์„ ํ•˜๊ฒŒ ๋˜์–ด ์ด๊ฑด ์•„๋‹ˆ๋‹ค!! ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด ๋‹ค์‹œ ๊ณ ๋ฏผ์„ ํ•ด ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ ‘๊ทผ์„ ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. 

( + ํ’€๊ณ ๋‚˜์„œ ๋‹ค๋ฅธ ํ’€์ด๋“ค์„ ๋ณด๋‹ˆ๊นŒ ๋ฆฌ์ŠคํŠธ ํ•˜๋“œ์ฝ”๋”ฉ ํ•˜์‹  ๋ถ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ๊นŒ ๋„ˆ๋ฌด ๊น”๋”ํ•ด์„œ ๋†€๋žฌ๋‹ค)

( ++ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋‹ค๋ฅธ ํ’€์ด๋ณด๊ธฐ์—์„œ ๊ณต์œ ๋Š” ๋ชปํ•˜๋‚˜?)

 

โœ๏ธ ๋‚ด ํ’€์ด

def solution(survey, choices):
#     ์นด์šดํŠธ๋ฅผ ํ•ด์ค„ ๋”•์…”๋„ˆ๋ฆฌ
    result_dic = {'R': 0, 'T': 0, 'C': 0, 'F': 0, 'J': 0, 'M': 0, 'A': 0, 'N': 0}
    result_list = ["R", "T", "C", "F", "J", "M", "A", "N"]
    
    # 7๊ฐ€์ง€ ๋‹ต๋ณ€์— ๋งž์ถ”์–ด ์ ์ˆ˜ ์นด์šดํŠธ
    for i in range(len(survey)):
        if choices[i] < 4:
            result_dic[survey[i][0]] += 4 - choices[i]
        elif choices[i] > 4:
            result_dic[survey[i][1]] += choices[i] - 4
        # 4์ผ๋•Œ๋Š” pass
    
    answer = ""
    
    # 0๋ถ€ํ„ฐ 2์”ฉ ๋ฐ˜๋ณตํ•˜์—ฌ i, i+1 ์ฆ‰ ๋‘ ์œ ํ˜• ์ค‘ ๊ฒฐ๊ณผ ๊ฐ’์ด ๋  ์œ ํ˜•์„ ์ฐพ๊ธฐ
    for i in range(0, len(result_list), 2):
        # R : 6, T : 1 => answer += R
        if result_dic[result_list[i]] > result_dic[result_list[i+1]]:
            answer += result_list[i]
        elif result_dic[result_list[i]] < result_dic[result_list[i+1]]:
            answer += result_list[i+1]
        else:
            # ๋‘ ์œ ํ˜•์˜ ์นด์šดํŠธ๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ
            answer += result_list[i]
    return answer

 

 

๐Ÿ˜Š ํ›„๊ธฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ์—ฐ์Šต๋ฌธ์ œ๋Š” ์ฒ˜์Œ ํ’€์–ด๋ณด๋Š”๋ฐ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค๋Š” input์„ ๋”ฐ๋กœ ํ•˜์ง€ ์•Š๋Š” ๋‹ค๋Š” ์‚ฌ์‹ค์ด ์‹ ๊ธฐํ–ˆ๋‹ค!