/ 제출 1 /
import sys
input = sys.stdin.readline
from collections import deque
n = int(input())
queue = deque([i+1 for i in range(n)])
while True :
queue.popleft()
if len(queue) != 1:
queue.append(queue.popleft())
else:
print(queue.popleft())
break

헐. 진짜 설마설마하면서 잘 만들었다고 생각했는데 IndexError라니.. 진짜 멍충..
시간복잡도 걱정했는데 의외로 500,000을 넣어도 무리 없이 시간 안에 도는 것 같다.
예상한대로 한 번 계산 시마다 절반씩 값이 줄기 때문인 것 같다.
그래도 매번 IndexError 나니까 어디부터 검사해야할 지 알겠다.
보통 초기값 2-3개 or 끝값 2-3개 or Input의 초기값 or Input의 끝값 등
/ 제출 2 /
import sys
input = sys.stdin.readline
from collections import deque
n = int(input())
queue = deque([i+1 for i in range(n)])
while True :
if len(queue) > 1:
queue.popleft()
queue.append(queue.popleft())
else:
print(queue.popleft())
break

아니나 다를까 1이 Input으로 들어왔을 때, popleft()를 중복으로 해줘서, else:에서 queue가 비어있어서 런타임에러가 난 것 이었다.
제출 전에 좀 검사하면 안되나? 응? ㅎ?
'CodingTest > Baekjun Online Judge' 카테고리의 다른 글
[ BOJ / 파이썬 ] 5430 AC (0) | 2022.08.15 |
---|---|
[ BOJ / 파이썬 ] 1021 회전하는 큐 (0) | 2022.08.14 |
[ BOJ / 파이썬 ] 18258 큐2 (0) | 2022.08.14 |
[ BOJ / 파이썬 ] 2493 탑 (0) | 2022.08.14 |
[ BOJ / 파이썬 ] 1874 스택 수열 (0) | 2022.08.13 |