2 回答

TA貢獻1809條經(jīng)驗 獲得超8個贊
我注意到你打電話給你的區(qū)段,而實際上,它們不是。因此,為了消除任何混淆,我決定將它們重命名為,然后創(chuàng)建一個單獨的數(shù)組,該數(shù)組具有一些關(guān)于哪些段應(yīng)該首先出現(xiàn)的邏輯。orderedSegmentsunOrderedSegmentsorderedSegments
然后,我編寫了一個小的輔助函數(shù),該函數(shù)查看兩個段并決定它們是否相交/重疊。isOverlapping(a, b)
然后,我創(chuàng)建了一個數(shù)組,它將每個層作為單獨的元素(段數(shù)組)保存。layers
邏輯很簡單,如偽代碼中所述:
loop through each `segment` in `orderedSegments`:
loop through each `layer` in `layers`:
check if the `segment` is overlaping any of the segments in the current `layer'
if not then insert the `segment` to the current `layer`
if yes, then check with the other layers
if we reach the end of layers but could not insert it in any,
then add the segment to its own new layer
const unOrderedSegments = [
[15, 18], // 1
[0.3, 9], // 2
[4, 13], // 3
[8, 14], // 4
[1, 3], // 5
[16, 19.5], // 6
[4.1, 17.5], // 7
[0, 2.9], // 8
[2.9, 11], // 9
[12.5, 19.4], // 10
[11.3, 12], // 11
];
const orderedSegments = unOrderedSegments.sort((a, b) => a[0] - b[0]);
const isOverlapping = (a, b) => {
const check1 = a[0] > b[0] && a[0] < b[1];
const check2 = b[0] > a[0] && b[0] < a[1];
return check1 || check2;
};
const layers = [];
for (let i = 0; i < orderedSegments.length; i++) {
const currentSegment = orderedSegments[i];
let inserted = false;
for (let j = 0; j < layers.length; j++) {
const currentLayer = layers[j];
let canBeInserted = true;
for (let k = 0; k < currentLayer.length; k++) {
const segment = currentLayer[k];
if (isOverlapping(segment, currentSegment)) {
canBeInserted = false;
break;
}
}
if (canBeInserted) {
currentLayer.push(currentSegment);
inserted = true;
break;
}
}
if (!inserted) {
layers.push([currentSegment]);
}
}
// print each layer on a spearate line
// ( convert array to string )
layers.forEach((layer) => {
let layerString = "";
layer.forEach((segment) => {
layerString += `[${segment[0]}-${segment[1]}]`;
});
console.log(layerString);
});

TA貢獻1876條經(jīng)驗 獲得超7個贊
const orderedSegments = [
[15, 18], // 1
[0.3, 9], // 2
[4, 13], // 3
[8, 14], // 4
[1, 3], // 5
[16, 19.5], // 6
[4.1, 17.5], // 7
[0, 2.9], // 8
[2.9, 11], // 9
[12.5, 19.4], // 10
[11.3, 12] // 11
]
// sort array by starting time (orderedSegments[i][0])
orderedSegments.sort((a, b) => {
if(a[0] < b[0]) return -1;
if(a[0] > b[0]) return 1;
return 0;
});
const newSegments = [];
while(orderedSegments.length > 0) {
// get first element of array
let element = orderedSegments[0];
// all "used" items will be removed. used items are marked with -1
if(element[0] == -1) {
orderedSegments.shift();
break;
}
// newElementGroup represents a new layer
let newElementGroup = [];
newElementGroup.push(element);
for(let i = 0; i < orderedSegments.length; i++) {
if(orderedSegments[i][0] > element[1]) {
element = orderedSegments[i].slice();
newElementGroup.push(element);
// mark element as "used"
orderedSegments[i][0] = -1;
}
}
newSegments.push(newElementGroup);
// remove first element after creating a new layer until orderedSegments is empty
orderedSegments.shift();
}
newSegments.forEach(element => console.info(element))
我想這應(yīng)該可以解決問題
添加回答
舉報