-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathμμ°.js
56 lines (43 loc) Β· 885 Bytes
/
μμ°.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Solution 1
function solution1(d, budget) {
const _d = d.slice();
let totalCost = 0;
let i;
_d.sort((a, b) => a - b);
for (i = 0; i < d.length; i++) {
totalCost += _d[i];
if (totalCost > budget) {
break;
}
}
return i;
}
// Solution 2
function solution2(d, budget) {
const _d = d.slice();
let totalCost = 0;
let maxDepartmentCount = 0;
_d.sort((a, b) => a - b).forEach((cost) => {
totalCost += cost;
if (totalCost <= budget) {
maxDepartmentCount++;
}
});
return maxDepartmentCount;
}
// Solution 3
function solution3(d, budget) {
const _d = d.slice();
let totalCost = 0;
let maxDepartmentCount = 0;
_d.sort((a, b) => a - b);
for (const cost of _d) {
totalCost += cost;
if (totalCost <= budget) {
maxDepartmentCount++;
} else {
break;
}
}
return maxDepartmentCount;
}