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] 

+ Recent posts