๋ฌธ์ œ

๋ถ€์„œ๋ณ„๋กœ ์‹ ์ฒญํ•œ ๊ธˆ์•ก๋งŒํผ ๋ชจ๋‘ ์ง€์›ํ•ด์ค˜์•ผํ•œ๋‹ค. 

๋ถ€์„œ๋ณ„๋กœ ์‹ ์ฒญํ•œ ๊ธˆ์•ก์ด ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด d, ์˜ˆ์‚ฐ budget์ด ์ฃผ์–ด์งˆ ๋•Œ, 

์ตœ๋Œ€ ๋ช‡ ๊ฐœ์˜ ๋ถ€์„œ์— ๋ฌผํ’ˆ์„ ์ง€์›ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ return 

 

์ฝ”๋“œ

const solution = (d, budget) => {
  let answer = 0;
  // ๊ฐ€์žฅ ์ž‘์€ ์˜ˆ์‚ฐ์„ ๊ฐ€์ง„ ๋ถ€์„œ๋ถ€ํ„ฐ ์ด ์˜ˆ์‚ฐ์„ ์ฐจ๊ฐํ•ด๊ฐ€์•ผ ์ง€์›ํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ€์„œ๊ฐ€ ์ตœ๋Œ€๋กœ ๋œ๋‹ค. 
  d.sort((a, b) => a - b); // ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

  d.reduce((acc, cur) => {
    acc + cur <= budget ? answer++ : answer;
    return acc + cur;
  }, 0);

  return answer;
}

+ Recent posts