https://programmers.co.kr/learn/courses/30/lessons/42862
ํ์ด
์๊ณ ๋ฆฌ์ฆ : Greedy(ํ์๋ฒ)
# ํ๋ก๊ทธ๋๋จธ์ค 1๋จ๊ณ - ์ฒด์ก๋ณต
def solution(n, lost, reserve):
_reserve = [r for r in reserve if r not in lost]
_lost = [l for l in lost if l not in reserve]
for r in _reserve:
f = r - 1
b = r + 1
if f in _lost:
_lost.remove(f)
elif b in _lost:
_lost.remove(b)
return n - len(_lost)
def set_solution(n, lost, reserve):
newlost = set(lost) - set(reserve)
newreserve = set(reserve) - set(lost)
for i in newlost:
if i-1 in newreserve:
newreserve.remove(i-1)
elif i+1 in newreserve:
newreserve.remove(i+1)
else:
n-=1
return n
๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ฌ ํ์ด
์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ - Greedy
๋ ๋ฒ์งธ ๋ฐฉ๋ฒ - set