[LeetCode] 3178. Find the Child Who Has the Ball After K Seconds

Chobby·2025년 12월 24일

LeetCode

목록 보기
868/1063

😎풀이

  1. n 길이의 배열 생성
  2. k초간 반복
    2-1. 시작과 끝 지점까지 인덱스가 도달했다면, 반대 방향으로 방향 변경
    2-2. 정해진 방향으로 이동
  3. k초가 지난 후의 인덱스 반환
function numberOfChild(n: number, k: number): number {
    const child = Array.from({ length: n }, (_, i) => i)
    let curIdx = 0
    let curSec = 0
    let isRight = true
    while(curSec < k) {
        curSec++
        if(isRight) curIdx++
        else curIdx--
        if(curIdx === (n - 1) || (curSec !== 0 && curIdx === 0)) {
            isRight = !isRight
        }
    }
    return child[curIdx]
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글