private void insert(List<int[]> spans, int[] target) {
        int insertIndex = -1;

        if (spans.isEmpty()) {
            spans.add(target);
            return;
        }

        for (int i = 0; i < spans.size(); i++) {
            int[] span = spans.get(i);

            if (!(target[1] < span[0] - 1 || target[0] > span[1] + 1)) {
                span[0] = Math.min(span[0], target[0]);
                span[1] = Math.max(span[1], target[1]);

                target = span;
                if (insertIndex != -1) {
                    spans.remove(insertIndex);
                }

                insertIndex = i;
            }
        }

        if (insertIndex == -1) {
            int[] firstSpan = spans.get(0);

            if (target[0] < firstSpan[0]) {
                spans.add(0, target);
            } else {
                spans.add(target);
            }
        }
    }