Algorithm Problem/Python

[python] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ(2022 KAKAO BLIND RECRUITMENT)

deo2kim 2022. 2. 3. 20:19
๋ฐ˜์‘ํ˜•

๐Ÿค”๋ฌธ์ œ ํ•ด๊ฒฐ

์‹ ๊ณ ๋‹นํ•œ ์‚ฌ๋žŒ์„ ๊ธฐ์ค€์œผ๋กœ ์‹ ๊ณ ์ž๋ฅผ set ์œผ๋กœ ๋‹ด์•„์ค€๋‹ค( set์„ ์‚ฌ์šฉํ•œ ์ด์œ ๋Š” ์ค‘๋ณต ์‹ ๊ณ ๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ)
์‹ ๊ณ ์ž๊ฐ€ ๋‘๋ช… ์ด์ƒ์ด๋ฉด ์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ์„ ์ฐพ์•„ ๋ฉ”์ผ ํšŸ๋ฃจ๋ฅด +1 ์”ฉ ํ•ด์ค€๋‹ค.

๐Ÿ’จ ๋‹ค๋ฅธ ํ’€์ด์˜ ๊ฒฝ์šฐ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ํ’€์–ด์„œ ๋ฉ”๋ชจ๋ฆฌ์—์„œ๋Š” ์ด์ ์ด ์žˆ๊ณ  ์†๋„๊ฐ€ ๋Š๋ฆฐ ๋‹จ์ ์ด ์žˆ์ง€๋งŒ, ๋‚˜์˜ ๊ฒฝ์šฐ์— ๋”•์…”๋„ˆ๋ฆฌ ์“ฐ๋Š” ๊ฒƒ์„ ์ข‹์•„ํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋” ์‚ฌ์šฉํ•˜์ง€๋งŒ ์†๋„๋Š” 10๋ฐฐ ๋นจ๋ž๋‹ค(ex. 3๋ฒˆ๋ฌธ์ œ 1000ms vs 100ms)

 

๐Ÿ’ป์†Œ์Šค ์ฝ”๋“œ

def solution(id_list, report, k):
    answer = []
    reported_dict = {id: {'reporter': set(), 'mail_cnt': 0} for id in id_list}

    for r in report:
        reporter, reported_person = r.split(' ')
        reported_dict[reported_person]['reporter'].add(reporter)

    for key, val in reported_dict.items():
        if len(val['reporter']) >= k:
            for reporter in reported_dict[key]['reporter']:
                reported_dict[reporter]['mail_cnt'] += 1

    for val in reported_dict.values():
        answer.append(val['mail_cnt'])
    return answer

 

๐Ÿ“•๋ฌธ์ œ ํ™•์ธ

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

๋งํฌ: ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

๋ฐ˜์‘ํ˜•