# [Algorithm/JS] νλ‘κ·Έλλ¨Έμ€ 12982 μμ°
π λ¬Έμ λ°λ‘κ°κΈ° (opens new window)
# Solution
function solution(d, budget) {
let answer = 0;
let sum = 0;
d.sort((a, b) => a - b);
for (let i = 0; i < d.length; i++) {
if (sum + d[i] <= budget) {
sum += d[i];
answer++;
}
}
return answer;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
μ€λ¦μ°¨μμΌλ‘ μ λ ¬νμ¬ ν© μ°μ°μ ν΅ν΄ νλ©΄ μ½κ² ν΄κ²°ν μ μλ€.
sort
ν¨μλ₯Ό μ¬μ©ν΄ μ€λ¦μ°¨μμΌλ‘ μ λ ¬ ν λ°°μ΄ d μ 첫λ²μ§Έ μΈλ±μ€λΆν° λν κ°μ΄ μμ°(budget)λ³΄λ€ μκ±°λ κ°μ λ κΉμ§ μ°¨λ‘λ‘ λν΄λκ°λ€.
λν λ λ§λ€ answer λ₯Ό 1μ© μ¦κ°ν΄μ£Όλ©΄ ν©ν κ°μλ₯Ό λ°νν μ μλ€.