😎풀이

  1. time회 반복
    1-1. 마지막 사람에게, 다시 처음 사람에게 전달된 경우 방향 변경 지시
    1-2. 방향에 따라 다음 사람에게 전달
  2. 마지막 베개를 들고있는 사람의 인덱스 반환 (1-Indexed)
function passThePillow(n: number, time: number): number {
    let toRight = true
    let curHold = 1
    for(let i = 1; i <= time; i++) {
        if(i === 1) {
            curHold++
            continue
        }
        if(curHold === n || curHold === 1) toRight = !toRight
        if(toRight) curHold++
        else curHold--
    }
    return curHold
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글