-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler-task.js
93 lines (77 loc) · 2.74 KB
/
scheduler-task.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function scheduler(activities) {
const unsortedActivities = Array.from(activities);
activities.sort((a, b) => a.start - b.start || a.end - b.end);
let loraineEnd = -1;
let charlesEnd = -1;
let assignment = [];
for (let activity of activities) {
const { start, end } = activity;
const resultPosition = unsortedActivities.indexOf(activity);
if (start >= charlesEnd) {
assignment.splice(resultPosition, 0, 'C');
charlesEnd = end;
} else if (start >= loraineEnd) {
assignment.splice(resultPosition, 0, 'J');
loraineEnd = end;
} else {
return "IMPOSSIBLE";
}
}
return assignment.join('');
}
// Processing each test case
function processTestCases(testCases) {
let results = [];
for (let i = 0; i < testCases.length; i++) {
const activities = testCases[i];
const result = scheduler(activities);
results.push(`Case #${i + 1}: ${result}`);
}
return results;
}
// Main function
function main() {
let testCases = [];
let currentTestCase = [];
let numberOfTestCases = 0;
let numberOfActivities = 0;
let currentActivityCount = 0;
rl.question('Enter the number of test cases, T: ', (testCount) => {
numberOfTestCases = parseInt(testCount);
const readActivity = () => {
rl.question('', (line) => {
if (currentActivityCount === 0) {
// Reading the number of activities for the current test case
numberOfActivities = parseInt(line);
currentActivityCount++;
readActivity();
} else {
// Reading an activity's start and end time
const [start, end] = line.split(' ').map(Number);
currentTestCase.push({ start, end });
currentActivityCount++;
if (currentActivityCount > numberOfActivities) {
testCases.push(currentTestCase);
currentTestCase = [];
currentActivityCount = 0;
}
if (testCases.length < numberOfTestCases) {
readActivity();
} else {
// All test cases are read, process them
const results = processTestCases(testCases);
results.forEach(result => console.log(result));
rl.close();
}
}
});
};
readActivity();
});
}
main();