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

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฒด์œก๋ณต - Python

https://programmers.co.kr/learn/courses/30/lessons/42862

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ฒด์œก๋ณต

์ ์‹ฌ์‹œ๊ฐ„์— ๋„๋‘‘์ด ๋“ค์–ด, ์ผ๋ถ€ ํ•™์ƒ์ด ์ฒด์œก๋ณต์„ ๋„๋‚œ๋‹นํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ ์—ฌ๋ฒŒ ์ฒด์œก๋ณต์ด ์žˆ๋Š” ํ•™์ƒ์ด ์ด๋“ค์—๊ฒŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ฃผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ํ•™์ƒ๋“ค์˜ ๋ฒˆํ˜ธ๋Š” ์ฒด๊ฒฉ ์ˆœ์œผ๋กœ ๋งค๊ฒจ์ ธ ์žˆ์–ด, ๋ฐ”๋กœ ์•ž๋ฒˆ

programmers.co.kr

 

 

 

 

def solution(n, lost, reserve):
    answer = []
    #์ฒด์œก๋ณต์„ ์žƒ์–ด๋ฒ„๋ฆฐ ํ•™์ƒ ์ค‘ ์—ฌ๋ฒŒ์ด ์—†๋Š” ํ•™์ƒ =์ฐธ๊ฐ€๋ถˆ๊ฐ€
    set_lost = set(lost)-set(reserve)
    #์ฒด์œก๋ณต ์—ฌ๋ฒŒ์ด ์žˆ๋˜ ํ•™์ƒ ์ค‘ ์žƒ์–ด๋ฒ„๋ฆฐ ํ•™์ƒ๋นผ๊ณ  =๋นŒ๋ ค์ค„์ˆ˜์žˆ๋Š” ํ•™์ƒ
    set_reserve = set(reserve)-set(lost)

    #์ฐธ๊ฐ€ ๊ฐ€๋Šฅํ•œ ํ•™์ƒ ์ค‘
    for i in set_reserve:# ๊ทธ ์™ผ์ชฝ์ด ์ฒด์œก๋ณต์ด ์—†๋Š” ๊ฒฝ์šฐ
        if i-1 in set_lost:
            set_lost.remove(i-1)
        elif i+1 in set_lost: #๊ทธ ์˜ค๋ฅธ์ชฝ์ด ์—†๋Š” ๊ฒฝ์šฐ
            set_lost.remove(i+1) 
    answer = n-len(set_lost)
    return answer