프로그래머스 LV2 점프와 순간 이동

dowon kim·2023년 8월 24일
1

function solution(N) {
    let battery = 0; // 건전지 사용량 초기화

    while (N > 0) { // 거리 N이 0이 될 때까지 반복
        if (N % 2 === 1) { // 거리 N이 홀수인 경우
            battery += 1; // 건전지 사용량 1 증가
            N -= 1;       // 점프로 1만큼 거리 감소
        }
        N /= 2; // 순간 이동으로 거리를 2로 나눔 (건전지 사용량 증가 없음)
    }

    return battery; // 최소 건전지 사용량 반환
}
profile
The pain is so persistent that it is like a snail, and the joy is so short that it is like a rabbit's tail running through the fields of autumn

0개의 댓글