-
99클럽 코테 스터디 13일차 TIL: 스택/큐, Leetcode 1823. Find the Winner of the Circular GameToday_I_Learned/Algorithm 2024. 6. 22. 18:32
문제
https://leetcode.com/problems/find-the-winner-of-the-circular-game/description/
통과 여부
Pass
나의 접근법
- 1 부터 n 까지 구성된 List 'q' 생성
- 매 Turn 마다 k 번째에 있는 요소 탐색 => target = k % len(q) (만약 index 가 0부터 시작이면 -1 을 해주어야 함.)
- 매 Turn 마다 target 을 제거하고 q 를 갱신. q = 'target 이후의 List' + 'target 이전까지의 List'
- q 의 길이가 1이 될 때까지 반복
class Solution: def findTheWinner(self, n: int, k: int) -> int: q = [e+1 for e in range(n)] while len(q) > 1: target = k % len(q) - 1 # target 위치 찾기. index 가 0부터 시작하므로 '-1'. if target >= 0: q = q[target+1:] + q[:target] # 중간에 위치한 target 을 빼고 부분 List 연결 else: # target 이 0보다 작다 = target 이 마지막 순서임. q = q[:target] # 따라서 q 를 target 전 까지의 List 로 갱신 return q[0]
'Today_I_Learned > Algorithm' 카테고리의 다른 글