- 파이썬에서 제공하는 deque 자료구조를 가지고 원형 queue를 구현 해보는 문제
- 처음에는 파이썬에서 제공하는 deque을 이용하지 않고 포인터를 쓴다는 개념으로 접근해서 풀이했다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import sys
sys.stdin = open("input", "rt")
n, k = map(int, input().split())
queue = list(range(1, n+1))
pointer=0
cnt=0
while len(queue) > 1:
if pointer==len(queue):
pointer=0
if cnt==k-1:
queue.pop(pointer)
cnt=0
continue
pointer+=1
cnt+=1
print(queue.pop())
|
cs |
- deque을 사용한 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import sys
from collections import deque
# sys.stdin = open("input", "rt")
n, k = map(int, input().split())
deq = list(range(1, n+1))
deq = deque(deq)
while deq:
for _ in range(k-1):
temp=deq.popleft()
deq.append(temp)
deq.popleft()
if len(deq)==1:
print(deq.popleft())
|
cs |
728x90
'Data Structures & Algorithms' 카테고리의 다른 글
[Algorithms] 스택,큐,해쉬,힙-최소힙 & 최대힙 (0) | 2022.05.19 |
---|---|
[Algorithms] 스택,큐,해쉬,힙-아나그램(해쉬) (0) | 2022.05.18 |
[Algorithms] 스택,큐,해쉬,힙-후위 표기식 만들기(스택) (0) | 2022.05.18 |
[Algorithms] 스택,큐,해쉬,힙-쇠막대기(스택) (0) | 2022.05.17 |
[Algorithms] 스택,큐,해쉬,힙-가장 큰 수(스택) (0) | 2022.05.17 |