728x90

나의 풀이
def solution(n, lost, reserve):
lost, reserve = set(lost) - set(reserve), set(reserve) - set(lost)
for student in lost:
if student - 1 in reserve:
reserve.remove(student - 1)
elif student + 1 in reserve:
reserve.remove(student + 1)
else:
n -= 1
return n
list 안 for문을 사용한 풀이
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)반응형
'Coding Test > Programmers' 카테고리의 다른 글
| [프로그래머스/파이썬] 큰 수 만들기 (0) | 2022.06.07 |
|---|---|
| [프로그래머스/파이썬] 조이스틱 (0) | 2022.06.06 |
| [프로그래머스/파이썬] 카펫 (0) | 2022.06.04 |
| [프로그래머스/파이썬] 소수 찾기 (0) | 2022.06.04 |
| [프로그래머스/파이썬] 모의고사 (0) | 2022.06.04 |