# 22. 数据最节约的备份方法
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const lines = [];
rl.on('line', (line) => {
let files = line.split(',').map(Number);
files.sort();
let min = 0;
let max = files.length + 1;
while(min < max) {
let mid = Math.floor((min + max)/2);
if (check(mid, files)) {
max = mid;
} else {
min = mid + 1;
}
}
console.log(min);
});
function check(mid, files){
let discs = new Array(mid).fill(500);
for(let i=files.length-1; i>=0; i--) {
let size = files[i];
discs.sort();
if(discs[mid-1]>=size) {
discs[mid-1] -= size;
} else {
return false;
}
}
return true;
}
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
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
← 21. 数字游戏 23. 文件缓存系统 →