[LeetCode] 2682. Find the Losers of the Circular Game

Chobby·4일 전

LeetCode

목록 보기
781/800

😎풀이

  1. 플레이어 선언 (0 인덱스 방식)
  2. 두 번 지목된 플레이어 발생 시점까지 순회
    2-1. 현재 플레이어가 지목되었음을 가정하고, 목록에서 제외
    2-2. 다음 플레이어 번호 계산
  3. 목록에 남은 플레이어는 한 번도 호명되지 않은 플레이어 이므로, 1인덱스 방식으로 변환하여 반환
function circularGameLosers(n: number, k: number): number[] {
    const players = Array.from({ length: n }, (_, i) => i)
    const playerSet = new Set(players)
    let next = 0
    let step = k
    let idx = 1
    while(playerSet.has(next)) {
        playerSet.delete(next)
        next = (next + step) % (n)
        step = k * ++idx
    }
    return Array.from(playerSet).map(num => num + 1)
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글