Algorithm/programmers

택배 상자 꺼내기 ( Level 1, JavaScript, 2025 프로그래머스 코드챌린지 2차 예선 )

takeU 2025. 2. 13. 18:34
반응형
const solution = (n, w, num) => {
    let res = 1
    const total = Math.ceil(n / w)
    const target = Math.ceil(num / w)
    const nRemain = n % w || n
    const numRemain = num % w || num
    if (total % 2 === target % 2 && nRemain < numRemain) res -= 1 
    if (total % 2 !== target % 2 && nRemain + numRemain <= w) res -= 1
    return total - target + res
}

total - 전체 쌓여지는 층 수
target - num이 쌓인 층 수

total과 target이 같은 방향인지, 반대 방향인지 확인 후 타겟의 위에 상자가 있는지 체크하는 코드를 통해 위에 없는 경우 -1을 해줌