python-algorithm

백준 2164 카드 2

무적김두칠 2021. 8. 24. 10:54

1
2
3
4
5
6
7
8
from collections import deque
n=int(input())
tmp=deque([i+1 for i in range(n)])
while len(tmp)>1:
    tmp.popleft()
    tmpNum=tmp.popleft()
    tmp.append(tmpNum)
print(tmp[0])
cs
1
2
3
4
5
6
7
from collections import deque
n=int(input())
tmp=deque([i+1 for i in range(n)])
while len(tmp)>1:
    tmp.popleft()
    tmp.rotate(-1)
print(tmp[0])
cs

제일 처음 생각했던건 리스트를 써서 pop을 두번하자 였는데 시간초과 나오더라구요
그래서 collections에 있는 deque를 써서 했고

두번째 코드같은경우에는 deque 안에 rotate 라는 함수를 써서 
deque.rotate(-1)은 왼쪽으로 이동
deque.roate(1)은 오른쪽으로 이동 하는 기능도 있어서 같이 올려드립니다.

밑에 코드 출처입니다.
https://velog.io/@pmk4236/%EB%B0%B1%EC%A4%80-2164%EB%B2%88-%EC%B9%B4%EB%93%9C-2-Python

 

반응형