Find the Winner of the Circular Game - LeetCode
class Solution:
def findTheWinner(self, n: int, k: int) -> int:
d = collections.deque([i for i in range(1, n + 1)])
while(len(d) != 1):
d.rotate(-k)
d.pop()
return d.pop()
dedque.rotate( )로 리스트 회전하기
: 괄호 안에 양수를 넣으면 오른 쪽으로 밀리고, 양수를 넣으면 왼 쪽으로 밀린다.
arr = [1, 2, 3, 4, 5] 였다면
arr.rotate(2) = [4, 5, 1, 2, 3]
arr.rotate(-2) = [3, 4, 5, 1, 2]
'알고리즘 PS > data structure' 카테고리의 다른 글
파이썬에서 해시맵 구현은 딕셔너리 (0) | 2024.07.26 |
---|---|
921. Minimum Add to Make Parentheses Valid (0) | 2024.06.25 |
1512. Number of Good Pairs 파이썬 defualtdict 이용해서 해시로 풀기 (0) | 2024.06.15 |
그래프 구현 기본 (0) | 2024.05.30 |
스택 - 프로그래머스 올바른 괄호 (파이썬) (0) | 2024.05.23 |