이 글은,
1. 미래의 내가 다시 이 문제를 풀고자 할 때 과거의 내가 어떻게 문제를 해결했었는지 알려주기 위해서
2. 같은 문제를 풀고 있는 사람에게 아이디어를 제공하기 위해서
작성되었습니다.
백준 11866 요세푸스 문제 0 : https://www.acmicpc.net/problem/11866
💡 아이디어
- 앞의 수를 뽑아서 뒤로 다시 넣어주는 규칙 -> 큐를 떠올릴 수 있다.
from collections import deque
dq = deque()
N, K = map(int, input().split())
array = []
for i in range(1, N+1):
array.append(i)
answer = []
flag = [0]*N
cnt = 0
while 0 in flag:
for num in array:
if flag[num-1] == 0:
dq.append(num)
cnt += 1
if cnt % K == 0 and cnt != 0:
target = dq.pop()
answer.append(str(target))
flag[target-1] = 1
print(f"<{', '.join(answer)}>")
from collections import deque
import sys
# 인스턴스 생성
q = deque()
# 명령 실행
N, K = map(int, sys.stdin.readline().split())
for i in range(1,N+1):
q.append(i)
print('<',end='')
while len(q)>1:
for _ in range(K-1):
q.append(q.popleft())
x = q.popleft()
print(f'{x}, ',end='')
x = q.popleft()
print(f'{x}>')
이렇게 간단한 문제를 저렇게 복잡하게 풀었다니 😱😱😱
시간 차이도 3배 가까이 난다.
팀원들이 있어서 다행이다.🐹