区间数组合并代码输出异常:不符预期结果的问题排查求助
Fixing Interval Merging Logic
Your current code is producing incorrect results because it doesn't account for all possible overlap scenarios, especially when merging creates a new interval that overlaps with existing ones in your accumulator. Let's break down the issues and fix the code.
Why Your Code Fails
Let's walk through your example data [[0, 33], [66, 80], [0, 66], [33, 100]]:
- After processing
[0,66], your accumulator becomes[[0,66], [66,80]]. - When processing
[33,100], you merge it with[0,66]to get[0,100], then break out of the loop immediately. This means you never check if[0,100]overlaps with[66,80](which it does completely), leaving the redundant interval in the result.
Other key issues:
- No sorting: Without sorting intervals by their start time, overlapping intervals aren't guaranteed to be adjacent, making it hard to catch all merges.
- Early loop termination: Breaking after the first merge prevents checking if the newly merged interval overlaps with others in the accumulator.
Correct Approach: Sort First, Then Merge
The standard and efficient way to merge intervals is:
- Sort intervals by their start value.
- Iterate through sorted intervals, merging each with the last merged interval if they overlap.
Here's the fixed code:
const data = [ [0, 33], [66, 80], [0, 66], [33, 100] ]; const createDataForSlider = (data) => { // Handle empty input case if (data.length === 0) return []; // Sort intervals by their start time const sortedIntervals = [...data].sort((a, b) => a[0] - b[0]); // Use reduce to build merged intervals return sortedIntervals.reduce((merged, current) => { const lastMerged = merged[merged.length - 1]; // Check if current interval overlaps with the last merged one if (current[0] <= lastMerged[1]) { // Merge them by updating the end to the maximum of both ends lastMerged[1] = Math.max(lastMerged[1], current[1]); } else { // No overlap, add current interval to merged list merged.push(current); } return merged; }, [sortedIntervals[0]]); // Initialize with first sorted interval }; console.log(createDataForSlider(data)); // Output: [[0, 100]]
How It Works
- Sorting: By sorting intervals by their start time, we ensure that any overlapping intervals are adjacent, so we only need to check the last merged interval each time.
- Reduce Logic:
- Start with the first sorted interval as the initial merged list.
- For each subsequent interval:
- If it overlaps with the last merged interval (current start ≤ last merged end), merge them by updating the end to the larger of the two ends.
- If no overlap, add it to the merged list as a new interval.
- Edge Cases: Handles empty input, non-overlapping intervals, and completely enclosed intervals correctly.
Testing with your examples:
- Input
[[0, 33], [66, 80]]→ Output[[0, 33], [66, 80]](correct, no overlaps). - Input
[[0, 33], [66, 80], [0, 66], [33, 100]]→ Output[[0, 100]](correct, all intervals merge into one).
内容的提问来源于stack exchange,提问作者Александр Кос
相关产品推荐
相关产品推荐

