ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค [lv1] ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜ ํŒŒ์ด์ฌ

2021. 6. 30. 21:41ใ†๐Ÿ”ฑ Algorithm

 

๐ŸŒŠ ์ ‘๊ทผ

 

1. ์ฐธ๊ฐ€์ž์™€ ์™„์ฃผ์ž๋ฅผ ๊ฐ dictionary ์— ์ €์žฅ. 

2. value ๊ฐ’์ด 2 ์ด์ƒ ๋‚˜์˜ค๋Š” key ๋Š”, ๋™์ผํ•œ ์ด๋ฆ„์˜ ์ฐธ๊ฐ€์ž๊ฐ€ ์กด์žฌํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

3. ์ด ์ ์„ ์œ ์˜ํ•˜์—ฌ, ๋™์ผํ•œ key ์— ๋Œ€ํ•ด ๊ฐ dictionary ์˜ value ๊ฐ’์ด ๋™์ผํ•œ์ง€, ๊ทธ๋ ‡์ง€ ์•Š์€์ง€ ์ฐพ์•„๋‚ธ๋‹ค.

# ํ†ต๊ณผ
def solution(participant, completion):
    answer = ''
    parti = dict()
    comple = dict()

    for name in participant:
        try:
            parti[name] += 1
        except KeyError:
            parti[name] = 1

    for name in completion:
        try:
            comple[name] += 1
        except KeyError:
            comple[name] = 1

    for name in parti:
        if parti[name] > 1:
            if parti[name] == comple[name]:
                pass
            else:
                answer = name
                break
        elif name not in comple:
            answer = name
            break
    return answer

 

 

โ„๏ธ ์œ ์˜ ์‚ฌํ•ญ

 

participant ๋Š” ์ตœ๋Œ€ 10๋งŒ์ด๋ฏ€๋กœ  for ๋ฌธ์„ ๊ทธ๋Œ€๋กœ ๋Œ๋ฆฌ๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ์˜ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค. ์•„๋ž˜์˜ ํ’€์ด๋Š” participant ์—์„œ ์ผ์ผ์ด ์›์†Œ๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ, ์™„์ฃผ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•˜๋Š”๋ฐ ์ด๋Š” ์‹œ๊ฐ„ ์ดˆ๊ณผ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

 

# ์ผ€์ด์Šค ํ†ต๊ณผ/ ํšจ์œจ์„ฑ ์‹คํŒจ
def solution(participant, completion):
    comple = dict()

    for name in completion:
        try:
            comple[name] += 1
        except KeyError:
            comple[name] = 1

    for name in participant:
        if name not in comple:
            return name

        if participant.count(name) != comple[name]:
            return name


์ด ๋ฌธ์ œ์˜ ํฌ์ธํŠธ๋Š” participant ์˜ ์ค‘๋ณต๋˜๋Š” ์›์†Œ์ด๋‹ค.hash ๋ฅผ ์ด์šฉํ•˜๋ฉด ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜์—ฌ ์—ฐ์‚ฐํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ํšจ์œจ์„ฑ์„ ๊ทน๋ณตํ•  ์ˆ˜ ์žˆ๋‹ค.