๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ (Python)

1. ๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ œ ๋งํฌ

  • ์ž…๋ ฅ
    • ์œ ์ €ID ์ •๋ณด
    • ์œ ์ €๊ฐ€ ์‹ ๊ณ ํ•œ ID ํšŸ์ˆ˜
    • k : k๋ฒˆ์ด์ƒ ์‹ ๊ณ ๋‹นํ•˜๋ฉด ๊ฒŒ์‹œํŒ ์ด์šฉ์ •์ง€
  • ์ถœ๋ ฅ : ์œ ์ €๋ณ„๋กœ ์ž์‹ ์ด ์‹ ๊ณ ํ•œ ์œ ์ € ์ค‘, ์ตœ์ข… ์ด์šฉ์ •์ง€๋ฅผ ๋‹นํ•œ ์œ ์ €์˜ ๋ช…์ˆ˜๋ฅผ ์„ธ์„œ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ return

2. ์ฝ”๋“œ

solution1.py

def solution(id_list, report, k):
    db = {name:[] for i, name in enumerate(id_list)}
    reports = {name:0 for i, name in enumerate(id_list)}

    for re in set(report):
        user = re.split(" ")[0]
        reported_user = re.split(" ")[1]
        db[user].append(reported_user)
        reports[reported_user] +=1

    answer = [0 for _ in range(len(id_list))]
    for key, values in db.items():
        for value in values:
            if reports[value] >=k:
                answer[id_list.index(key)] += 1
    return answer

3. ํšŒ๊ณ 

  • ํ•œ ์œ ์ €๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹ ๊ณ ํ•  ๊ฒฝ์šฐ, ๋™์ผํ•œ ์œ ์ €์— ๋Œ€ํ•œ ์‹ ๊ณ  ํšŸ์ˆ˜๋Š” 1ํšŒ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผํ•œ๋‹ค.
  • ์‹ ๊ณ ๋œ ํšŸ์ˆ˜๋ถ€๋ถ„์—์„œ report ๋ชฉ๋ก์ž์ฒด๋ฅผ set()ํ•ด์ฃผ๋ฉด๋˜๋Š”๋ฐ, if๋ฌธ์œผ๋กœ ์ด๋ฏธ ํ•œ ๋ฒˆ ์‹ ๊ณ ํ•œ ์ ์ด ์žˆ๋Š” ์œ ์ €์ธ์ง€๋ฅผ ๊ฒ€์‚ฌํ–ˆ์—ˆ๋‹ค๊ฐ€ ์ˆ˜์ •ํ–ˆ์–ด์„œ ์ˆ˜์ •ํ–ˆ๋‹ค.
  • ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, value๊ฐ’์„ ๋ฆฌ์ŠคํŠธ๋กœ ํ•˜๊ณ  ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” ์ดˆ๊ธฐํ™” ์‹œ์— ์ง€์ •ํ•ด์ฃผ๋ฉด ํŽธํ•˜๋‹ค.