You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

区间数组合并代码输出异常:不符预期结果的问题排查求助

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]]:

  1. After processing [0,66], your accumulator becomes [[0,66], [66,80]].
  2. 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:

  1. Sort intervals by their start value.
  2. 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

  1. 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.
  2. 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.
  3. 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,提问作者Александр Кос

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:53:32