heap

minkyung·2023년 11월 7일
0

17.5 / 100.0

function solution(no, works) {
  let result = 0;

  function compareNumbers(a, b) {
    return b - a;
  }

  do {
    works.sort(compareNumbers);
    const max = works[0]
    const min = works[works.length - 1]
    if ((max - min) > 0) {
      works[0] = works[0] - (max - min);
      no = no - (max - min);
    } else {
      works[0] = works[0] - 1;
      no = no - 1
    }
  } while (no > 0)

  works.forEach((item) => { result += Math.pow(item, 2) })

  return result;
}
profile
프론트엔드 개발자

0개의 댓글