[LeetCode] 2591. Distribute Money to Maximum Children

Chobby·2025년 11월 20일

LeetCode

목록 보기
767/800

😎풀이

  1. 최대 8달러를 보유할 수 있는 어린이 수 최대 탐색
  2. 8달러 보유가 가능한 어린이가 있을 경우 반복
    2-1. 돈이 남는 경우 최댓값을 낮춰 재탐색
    2-2. 모든 어린이에게 최소 1달러를 줄 수 없는 경우 최댓값을 낮춰 재탐색
    2-3. 4원을 갖게되는 어린이가 있는 경우 재탐색
  3. 8달러를 갖게 될 어린이가 없는 경우 -1 반환
  4. 8달러를 갖는 어린이의 수 반환환
function distMoney(money: number, children: number): number {
    let maxEight = Math.min(children, Math.floor(money / 8))
    while(maxEight >= 0) {
        const remainMoney = money - 8 * maxEight
        const remainChildren = children - maxEight
        if(remainMoney > 0 && remainChildren === 0) {
            maxEight--
            continue
        }
        if(remainMoney < remainChildren) {
            maxEight--
            continue
        }
        if(remainChildren === 1 && remainMoney === 4) {
            maxEight--
            continue
        }
        return maxEight
    }
    return -1
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글