-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm27.js
More file actions
37 lines (33 loc) · 888 Bytes
/
algorithm27.js
File metadata and controls
37 lines (33 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// function combination(arr, num){
// let res = []
// if(num === 1){return arr.map((v) => [v])}
// arr.forEach((e, i, arr) => {
// let comb = combination(arr.slice(i+1), num-1);
// res.push(...comb.map((v) => [e, ...v]));
// })
// return res
// }
// function solution(d, budget) {
// var answer = 0;
// for(let i=1; i<=d.length; i++){
// combination(d, i).map((val) => {
// let temp = val.reduce((s,c) =>s+c)
// if(temp <= budget && answer < i){answer = i}
// })
// }
// return answer;
// }
function solution(d, budget) {
let answer = 0
d = d.sort((a, b) => a - b)
let sum = 0
for(let i = 0; i<d.length; i++) {
if(sum + d[i] <= budget){
sum += d[i]
answer = i+1
} else {
break
}
}
return answer
}