반응형
잘 푼거같은데 자꾸 테스트 통과를 못해서
엄청 찾다가
인덱스가 문제라는걸 알았다
항상 range 든 list 든 탐색할때 0 부터 시작한다는걸 잊지말자!!
def solution(n, lost, reserve):
cloth = [1]*(n+1)
for i in range(1,n+1):
if i in lost and i in reserve:
cloth[i] = 1
continue
if i in lost:
cloth[i] = 0
if i in reserve:
cloth[i] = 2
print(cloth)
answer = 0
for i in range(1, n+1):
if cloth[i] == 0:
if i > 1:
if cloth[i - 1] == 2:
cloth[i] +=1
cloth[i - 1] -=1
continue
if i < n:
if cloth[i+1] ==2:
cloth[i] += 1
cloth[i+1] -=1
for i in range(1, n+1):
if cloth[i] > 0:
answer+=1
print(cloth)
return answer
반응형
'개발💻 > 알고리즘' 카테고리의 다른 글
[프로그래머스] 타겟 넘버 Python (0) | 2024.03.11 |
---|---|
[프로그래머스] 모의고사 Python (25) | 2024.03.09 |
[프로그래머스] 실패율 Python (0) | 2024.03.08 |
[프로그래머스] 성격 유형 검사하기 Python (29) | 2024.03.06 |
알고리즘 다시 시작... (2) | 2024.03.06 |