-
Notifications
You must be signed in to change notification settings - Fork 17
/
(hard) Super Computer.js
45 lines (39 loc) · 1.52 KB
/
(hard) Super Computer.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
/**
* Super Computer
* https://www.codingame.com/training/hard/super-computer
* Statement:
* The goal of this puzzle is to schedule the planning of different group of
* users for a super computer, so that as many experiments as possible can
* be run on it. Keep in mind that efficient solutions are sometime not so
* complex once the problem has been turned in the right way.
* Story:
* Your university research department recently acquired a brand new, shiny
* super computer. Problem is, everyone wants to use it. Some dude at the
* chemistry lab wants to run a one week experiment, your colleague Martin
* needs it for a "real quick try, I promise", etc... It's not going to be
* easy to have everyone satisfied with the planning.
* Your task will be to schedule the different experiments running on the
* computer so that as many experiments as possible can be run.
*/
let N = +readline();
const [calculation, agenda] = [[], []];
while(N--) {
const [J, D] = readline().split` `.map(n => +n);
calculation.push({start: J, end: J + D});
}
calculation
.sort((a, b) => a.start - b.start ? a.start - b.start : b.start - a.start)
.forEach(x => {
const {start, end} = x;
agenda.push(x);
for (let j = agenda.length - 2; j >= 0 && agenda[j].start <= start && agenda[j].end >= end; j--) {
agenda.splice(j, 1);
}
});
print(
agenda.reduce((x, y) => {
y.start >= x[x.length - 1].end && x.push(y);
return x;
},
agenda.splice(0, 1)).length
);