๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐˜ผ๐™ก๐™œ๐™ค๐™ง๐™ž๐™ฉ๐™๐™ข/๐™‹๐™ง๐™ค๐™œ๐™ง๐™–๐™ข๐™ข๐™š๐™ง๐™จ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค '2024 KAKAO WINTER INTERNSHIP๊ฐ€์žฅ ๋งŽ์ด ๋ฐ›์€ ์„ ๋ฌผ' ํŒŒ์ด์ฌ ํ’€์ด

by beomcoder 2024. 1. 8.
728x90
๋ฐ˜์‘ํ˜•

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

 

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

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

programmers.co.kr

 

def solution(friends, gifts):
    history = {name: {'friends': {k: 0 for k in friends}, 'score': 0} for name in friends}
    next_month_gift = {name: 0 for name in friends}
    
    for gift in gifts:
        give, get = gift.split()
        
        history[give]['friends'][get] += 1
        history[give]['score'] += 1
        
        history[get]['friends'][give] -= 1
        history[get]['score'] -= 1
    
    for name in history.keys():
        for fname, fscore in history[name]['friends'].items():
            if fscore > 0 or (fscore == 0 and history[name]['score'] > history[fname]['score']):
                next_month_gift[name] += 1

    return max(next_month_gift.values())

 

<ํ’€์ด>

history = {name: {'friends': {k: 0 for k in friends}, 'score': 0} for name in friends}
next_month_gift = {name: 0 for name in friends}

'''
์‚ฌ๋žŒ๋งˆ๋‹ค ์นœ๊ตฌ๋“ค์˜ ์ด๋ฆ„๊ณผ ์„ ๋ฌผ์„ ์ฃผ๊ณ ๋ฐ›์€ ๊ฐœ์ˆ˜, ์„ ๋ฌผ์ง€์ˆ˜๋ฅผ ์ ์„ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ๋‹ฌ์— ๋ฐ›์€ ์„ ๋ฌผ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋”•์…”๋„ˆ๋ฆฌ๋„ ๋งŒ๋“ค์—ˆ๋‹ค.
'''

 

for gift in gifts:
    give, get = gift.split()

    history[give]['friends'][get] += 1
    history[give]['score'] += 1

    history[get]['friends'][give] -= 1
    history[get]['score'] -= 1

'''
์„ ๋ฌผ์ •๋ณด๋ฆฌ์ŠคํŠธ๋ฅผ for๋ฌธ์œผ๋กœ ๋Œ๋ฉด์„œ
์„ ๋ฌผ์„ ์ค€์‚ฌ๋žŒ(give)๊ณผ ๋ฐ›์€์‚ฌ๋žŒ(get)์„ ๊ตฌ๋ถ„์‹œ์ผœ์ฃผ์—ˆ๊ณ ,

๊ธฐ๋ก์—์„œ ์„ ๋ฌผ์„ ์ค€์‚ฌ๋žŒ๊ณผ ๋ฐ›์€์‚ฌ๋žŒ์˜ ๊ด€๊ณ„์—์„œ +๋ฅผ ํ•ด์ฃผ์—ˆ๊ณ ,
์„ ๋ฌผ์„ ์ค€์‚ฌ๋žŒ์˜ ์„ ๋ฌผ์ง€์ˆ˜(score)๋ฅผ + ํ•ด์ฃผ์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์„ ๋ฌผ์„ ๋ฐ›์€ ์‚ฌ๋žŒ์€ ์„ ๋ฌผ์„ ์ค€ ์‚ฌ๋žŒ๊ณผ์˜ ๊ด€๊ณ„์—์„œ -๋ฅผ ํ•ด์ฃผ์—ˆ๊ณ ,
์„ ๋ฌผ์„ ๋ฐ›์€์‚ฌ๋žŒ์˜ ์„ ๋ฌผ์ง€์ˆ˜๋ฅผ - ํ•ด์ฃผ์—ˆ๋‹ค.

๋‘๋ช…์˜ ๊ด€๊ณ„์—์„œ ๋” ๋งŽ์ด ์ค€์‚ฌ๋žŒ์ด ๋‹ค์Œ๋‹ฌ์— ์„ ๋ฌผ์„ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋กํ•ด์ฃผ์—ˆ๊ณ ,
์„ ๋ฌผ์„ ์ฃผ๊ณ  ๋ฐ›์€ ํšŸ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์„ ๋ฌผ์ง€์ˆ˜๋ฅผ ๋น„๊ตํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋กํ•ด์ฃผ์—ˆ๋‹ค.
'''

 

for name in history.keys():
    for fname, fscore in history[name]['friends'].items():
        if fscore > 0 or (fscore == 0 and history[name]['score'] > history[fname]['score']):
            next_month_gift[name] += 1

return max(next_month_gift.values())

'''
๊ธฐ๋ก๋“ค์„ ์ „๋ถ€ ๋Œ๋ฉด์„œ, ์‚ฌ๋žŒ๋งˆ๋‹ค ์นœ๊ตฌ๋“ค์˜ ๊ด€๊ณ„๋ฅผ ๋น„๊ตํ•˜์—ฌ for๋ฌธ์„ ๋Œ์•˜๋‹ค.

์นœ๊ตฌ์—๊ฒŒ ์ค€ ์„ ๋ฌผํšŸ์ˆ˜๊ฐ€ ๋” ๋งŽ๊ฑฐ๋‚˜ (fscore > 0),
์„ ๋ฌผํšŸ์ˆ˜๊ฐ€ ๋™์ ์ด๊ณ , ๊ทธ ์นœ๊ตฌ์˜ ์„ ๋ฌผ์ง€์ˆ˜๋ณด๋‹ค ์ ์ˆ˜๊ฐ€ ๋†’๋‹ค๋ฉด 
(fscore == 0 and history[name]['score'] > history[fname]['score'])

๋‹ค์Œ๋‹ฌ์— ์„ ๋ฌผ๋ฐ›์„ ํšŸ์ˆ˜๋ฅผ 1 ์ฆ๊ฐ€์‹œ์ผœ์ฃผ์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ๋‹ฌ์˜ ์„ ๋ฌผ๋ฐ›์„ ์ˆ˜ ์ค‘ ๊ฐ€์žฅ ํฐ๊ฐ’์„ ๋ฆฌํ„ด์‹œ์ผœ์ฃผ์—ˆ๋‹ค.
'''
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€