Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- 토익문제
- ai consensus
- 토익공부
- pdf 다운로드
- 탑싯시험
- 데이터분석
- cv2
- 논문 pdf 이름
- 탑싯
- 데이터관련자격증
- model collapse
- TOPCIT
- students@ai seoul hackathon
- arXiv
- 토익
- toeic
- ADsP
- pip
- ai model collapse
- 코드프렌즈
- 환급형프론트챌린지
- 논문 pdf
- 미네르바 대학
- 프론트
- 토익공부법
- 크롬 확장프로그램
- 토익문법
- ai공모전
- scaico
- minerva university
Archives
- Today
- Total
토리의 데굴데굴 공부일기
[백준] 11866번 요세푸스 문제 0 Python 본문
https://www.acmicpc.net/problem/11866
11866번: 요세푸스 문제 0
첫째 줄에 N과 K가 빈 칸을 사이에 두고 순서대로 주어진다. (1 ≤ K ≤ N ≤ 1,000)
www.acmicpc.net
처음 생각한 풀이
queue를 돌려서 넘어가서 popleft하기..
import sys
n,k = map(int, sys.stdin.readline().split())
from collections import deque
queue = deque()
for i in range(1,n+1):
queue.append(i)
list = []
while len(queue) > 0:
for i in range(2):
queue.append(queue.popleft())
list.append(str(queue.popleft()))
print("<", ", ".join(list), ">", sep="")
근데 이렇게 하면 시간복잡도가 너무 커서 안됨
그래서 다른 분들꺼를 참고해보니 리스트로 큐를 구현해서 인덱싱을 사용해야 한다는걸 알게됨
아래는 수정한 코드
import sys
n,k = map(int, sys.stdin.readline().split())
queue = list(range(1,n+1))
index = 0
list = []
while len(queue) > 0:
index += (k-1)
if index >= len(queue):
index = index % len(queue)
list.append(str(queue.pop(index)))
print("<", ", ".join(list), ">",sep="")
한줄평
시간복잡도 고려랑 출력 형식 맞춰주는게 은근 빡셋음. join 함수랑 sep 복습하기!
'Algorithm' 카테고리의 다른 글
[알고리즘] ch06. 정렬 (0) | 2023.08.16 |
---|---|
[알고리즘] ch05. DFS,BFS (0) | 2023.08.10 |
[백준] 10162 전자레인지 Python (0) | 2023.08.06 |
[백준] 11399 ATM Python (0) | 2023.08.05 |
[알고리즘] ch03. 그리디 알고리즘(Greedy Algorithm) (0) | 2023.08.04 |