문제
https://programmers.co.kr/learn/courses/30/lessons/42862
나의 풀이
def solution(n, lost, reserve):
answer=0
count=0
student = []
for i in range(n):
student.append(1)
for i in range(len(lost)):
student[lost[i]-1] -= 1
for i in range(len(reserve)):
student[reserve[i]-1] += 1
for i in range(len(student)):
if i > 0 and i < len(student)-1:
if student[i] == 0:
if student[i-1] > 1:
student[i-1] -= 1
student[i] += 1
elif student[i+1] > 1:
student[i+1] -= 1
student[i] += 1
elif i == 0:
if student[i] == 0:
if student[i+1] > 1:
student[i+1] -= 1
student[i] += 1
elif i == len(student)-1:
if student[i] == 0:
if student[i-1] > 1:
student[i-1] -= 1
student[i] += 1
for i in student:
if i >= 1:
count += 1
answer = count
return answer
다른 사람의 풀이
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 solution(n, lost, reserve):
answer = 0
for i in range(1, n+1):
if i not in lost: #안 잃어버린 학생
answer += 1
else:
if i in reserve: #잃어버렸지만 여분도 있는 학생
answer += 1
reserve.remove(i)
lost.remove(i)
for i in lost: #잃어버리고 여분도 없어서 빌려야 하는 학생
if i-1 in reserve:
answer += 1
reserve.remove(i-1)
elif i+1 in reserve:
answer +=1
reserve.remove(i+1)
return answer
'Coding Test > 프로그래머스' 카테고리의 다른 글
[Level 2] - 타겟 넘버 (0) | 2022.03.06 |
---|---|
[Level 2] - 더 맵게 (0) | 2022.03.06 |
[Level 1] - K번째 수 (0) | 2021.11.09 |
[Level 1] - 완주하지 못한 선수 (0) | 2021.11.09 |
[Level 1] - 소수 만들기 (0) | 2021.11.09 |