-
Notifications
You must be signed in to change notification settings - Fork 0
/
findLongestGap.js
49 lines (36 loc) · 1.44 KB
/
findLongestGap.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
const findLongestGap = (roadLength, start, end) => {
//Array Of road Length
const roadLengthArray = Array(roadLength).fill(null).map((_, index) => index + 1);
//Array of Route Arrays
const carsRoute = start.reduce((acc, el, index) => {
acc[index] = ({ "start": el, "end": acc[index] });
return acc;
}, end);
//Array of uncovered Units of the Road
const uncoveredUnits = carsRoute.reduce((acc, el) => acc.filter((ac) => !((ac >= el.start) && (ac <= el.end))), roadLengthArray);
//Group the related roads
const groupUncoveredUnits = uncoveredUnits.reduce((acc, el, index) => {
//Init lastEl
if (index === 0) {
acc.arrays.push([el]);
}
if (acc.lastEl && ((acc.lastEl + 1) === el)) {
acc.arrays[acc.arrays.length - 1].push(el);
} else if (index !== 0) {
acc.arrays[acc.arrays.length] = [el];
}
acc.lastEl = el;
return acc;
}, { "arrays": [], "lastEl": null });
//Convert sub Arrays to numbers
const allGaps = groupUncoveredUnits.arrays.map((el) => el.length);
//Find the longest Gap
const longestGap = Math.max(...allGaps);
console.log(roadLengthArray, carsRoute, uncoveredUnits, groupUncoveredUnits, allGaps, longestGap);
return longestGap;
}
//Test the Function
const roadLength = 10;
const start = [1, 2, 5, 9];
const end = [1, 3, 6, 9];
findLongestGap(roadLength, start, end);