๊ธฐ๋ก๋ฐฉ

Lv.1 : ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ ๋ณธ๋ฌธ

CodingTest/Python

Lv.1 : ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

Soom_1n 2022. 9. 19. 14:32

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

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

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

programmers.co.kr



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์‹ ๊ณ ๋ฐ›์•„ ์ •์ง€๋œ ์œ ์ €๊ฐ€ ์žˆ์œผ๋ฉด, ์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ์—๊ฒŒ ์•Œ๋žŒ์ด๊ฐ„๋‹ค.
  • ์œ ์ €๋“ค์ด ๋ฐ›์€ ์•Œ๋žŒ ์ˆ˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

def solution(id_list, report, k):
    answer = {}
    for i in id_list:           # ์œ ์ € ID ๋ช…๋‹จ
        answer[i] = 0
    
    dic = {}
    for s in report:            # ์‹ ๊ณ  ํ˜„ํ™ฉ (์ค‘๋ณต์ œ๊ฑฐ)
        a, b = s.split()
        if b in dic.keys():
            dic[b].add(a)
        else:
            dic[b] = set()
            dic[b].add(a)

    for value in dic.values():  # ์•Œ๋žŒ ์นด์šดํŠธ
        if len(value) >= k:
            for i in value:
                answer[i] += 1

    return list(answer.values())

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์œ ์ € ID๋ช…๋‹จ์œผ๋กœ answer ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  • ์‹ ๊ณ  ํ˜„ํ™ฉ์„ dic ๋”•์…”๋„ˆ๋ฆฌ์— ์„ธํŠธ๋กœ ์ €์žฅํ•˜๋ฉฐ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ๋‹ค.
    • ์ž…๋ ฅ์€ ์‹ ๊ณ ์ž:์‹ ๊ณ  ๋ฐ›์€ ์œ ์ € ํ˜•์‹์ด์ง€๋งŒ, ์‹ ๊ณ  ๋ฐ›์€ ์œ ์ € : ์‹ ๊ณ ์ž ํ˜•ํƒœ๋กœ ๋ฐ”๊พผ๋‹ค.
  • dic ๋”•์…”๋„ˆ๋ฆฌ์—์„œ values์˜ ํฌ๊ธฐ๊ฐ€ k ์ด์ƒ์ด๋ฉด answer์˜ ๊ฐ’์„ 1 ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.

 

๐Ÿ”ธ ๋‹ค๋ฅธ ํ’€์ด ๐Ÿ”ธ

from collections import defaultdict, Counter

def solution(id_list, report, k):

    report_dict = defaultdict(set) 
    report_cntr = Counter()

    # {user1๊ฐ€ ์‹ ๊ณ ํ•œ user2๋“ค} ์ธ dict ์ƒ์„ฑ
    for r in report: 
        user1, user2 = r.split()
        report_dict[user1].add(user2)

    # ์‹ ๊ณ ๋‹นํ•œํšŸ์ˆ˜ ์ธ dict ์ƒ์„ฑ
    for v in report_dict.values():
        report_cntr.update(v)

    # {์‹ ๊ณ ๋ฅผ k๋ฒˆ ์ด์ƒ ๋‹นํ•œ ์‚ฌ๋žŒ๋“ค} set ์ƒ์„ฑ
    blacklist = {x for x in report_cntr if report_cntr[x] >= k} 

    # 1๊ณผ 3์„ ์ด์šฉํ•˜์—ฌ user1์ด ์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ๋“ค์ค‘์— k๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋‹นํ•œ ์‚ฌ๋žŒ๋“ค ์ˆซ์ž ๋„์ถœ
    return [len(report_dict[user] & blacklist) for user in id_list]

 

  • ์‹ ๊ณ  ํ˜„ํ™ฉ์„ ์ €์žฅํ•œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ defaultdict(set)๋ฅผ ์ด์šฉํ•ด set ํ˜•์‹์œผ๋กœ ์ƒ์„ฑํ•œ๋‹ค.
  • setํ˜•์‹ ๋”•์…”๋„ˆ๋ฆฌ์— ๊ฐ’์„ ๋„ฃ์œผ๋ฉฐ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ๋‹ค.
  • Counter() ๋”•์…”๋„ˆ๋ฆฌ์— ๊ฐ’์„ ๋„ฃ๊ณ , k๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋œ ๋ธ”๋ž™๋ฆฌ์ŠคํŠธ ์œ ์ €์˜ ID ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค.
  • ์ฒ˜์Œ setํ˜•์‹ ๋”•์…”๋„ˆ๋ฆฌ์™€ ๋งŒ๋“ค์–ด์ง„ ๋ธ”๋ž™๋ฆฌ์ŠคํŠธ ์œ ์ €๋ฅผ ๋น„๊ตํ•ด ๊ฒน์น˜๋Š” ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋”•์…”๋„ˆ๋ฆฌ์™€ ์„ธํŠธ ์‚ฌ์šฉ์„ ์—ฐ์Šตํ•ด๋ณผ ์ˆ˜ ์žˆ์—ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค.
  • ๋‹ค๋ฅธ ํ’€์ด์—์„œ defalutdict์™€ Counter ์‚ฌ์šฉ์„ ๋‹ค์‹œ๊ธˆ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
    • &๋กœ ๋‘ ๋ฆฌ์ŠคํŠธ ์›์†Œ๋ฅผ ๋น„๊ตํ•ด ๊ฐœ์ˆ˜๋ฅผ ํŒŒ์•…ํ•˜๋Š”๊ฒŒ ์ธ์ƒ์ ์ด์—ˆ๋‹ค.
    • ๋ฐ˜๋ณต๋ฌธ ์ˆ˜๊ฐ€ ์ ์–ด์„œ ๊ทธ๋Ÿฐ์ง€ ์ข€ ๋” ๋น ๋ฅธ ์ฒ˜๋ฆฌ๊ฐ€ ๋œ ํ…Œ์ŠคํŠธ๋“ค์ด ์žˆ์—ˆ๋‹ค.

728x90

'CodingTest > Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Lv.2 : ๋ฉ”๋‰ด ๋ฆฌ๋‰ด์–ผ  (0) 2022.09.20
Lv.2 : ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ  (0) 2022.09.19
Lv.2 : ์Šคํ‚ฌํŠธ๋ฆฌ  (0) 2022.09.18
BOJ_1012 : ์œ ๊ธฐ๋† ๋ฐฐ์ถ”  (0) 2022.09.15
Lv.2 : ํŠœํ”Œ  (0) 2022.09.12