LeetCode-056 consolidation interval

Posted by Serpent7 on Fri, 24 Dec 2021 23:24:54 +0100

Merge interval

Title Description: an array of intervals is used to represent a set of several intervals, in which a single interval is intervals[i] = [starti, endi]. Please merge all overlapping intervals and return a non overlapping interval array, which needs to cover all the intervals in the input.

See LeetCode's official website for an example.

Source: LeetCode
Link: https://leetcode-cn.com/problems/merge-intervals/
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Solution 1: recursion

The recursive process is as follows:

  • If intervals is empty or there is only one element in intervals, that is, there is only one interval, you do not need to merge and directly return intervals;
  • If intervals has more than one element, Declare a variable length to record the one-dimensional length of intervals (that is, how many intervals there are), the variable match records the number of intervals that do not need to be merged, matchFirst and matchSecond record the two numbers of intervals that need to be matched at present, and then declare a boolean array flag to record whether intervals have been merged, and then use the double loop to judge whether those intervals can be merged. The processing process is as follows:
    • The outer loop i starts from the first interval, matchFirst and matchSecond record two values of the corresponding interval of i, and match plus 1;
    • The inner loop j starts from the i + 1st interval, curFirst and curSecond record the two values of the corresponding interval of j, and then use matchFirst, matchSecond, curFirst and curSecond to judge whether there is an intersection between i and j. if there is an intersection, update the two numbers of i interval, update matchFirst and matchSecond, and mark the interval of j as true, that is, it has been merged; If there is no intersection, the next one is processed;
  • After the double loop processing, judge whether match and length are equal. If they are equal, it indicates that there is no interval that can be merged, and returns intervals; If they are not equal, initialize a new two-dimensional array newIntervals, copy the intervals in the intervals that have not been merged (judge whether they have been merged according to the flag array) to newIntervals, and then call merge(newIntervals) recursively.
public class LeetCode_056 {
    public static int[][] merge(int[][] intervals) {
        if (intervals == null || intervals.length == 1) {
            return intervals;
        }
        int length = intervals.length, match = 0, matchFirst, matchSecond;
        boolean[] flag = new boolean[length];

        for (int i = 0; i < length; i++) {
            if (!flag[i]) {
                matchFirst = intervals[i][0];
                matchSecond = intervals[i][1];
                match++;
                for (int j = i + 1; j < length && !flag[j]; j++) {
                    int curFirst = intervals[j][0], curSecond = intervals[j][1];
                    if (((matchFirst >= curFirst && matchFirst <= curSecond) || (matchSecond >= curFirst && matchSecond <= curSecond)) ||
                            ((curFirst >= matchFirst && curFirst <= matchSecond) || (curSecond >= matchFirst && curSecond <= matchSecond))) {
                        // Intersection
                        matchFirst = Math.min(matchFirst, curFirst);
                        matchSecond = Math.max(matchSecond, curSecond);
                        intervals[i][0] = matchFirst;
                        intervals[i][1] = matchSecond;
                        flag[j] = true;
                    }
                }
            }
        }
        if (match == length) {
            return intervals;
        }
        int[][] newIntervals = new int[match][2];
        for (int i = 0, j = 0; i < length; i++) {
            if (!flag[i]) {
                newIntervals[j] = intervals[i];
                j++;
            }
        }
        return merge(newIntervals);
    }
    
    public static void main(String[] args) {
        int[][] intervals = new int[][]{{1, 4}, {2, 3}};
        for (int[] ints : merge(intervals)) {
            for (int anInt : ints) {
                System.out.print(anInt + " ");
            }
            System.out.println();
        }
    }
}

[daily message] when you are unhappy, you can eat a piece of candy and tell yourself that life is still sweet. Come on.

Topics: Java Algorithm leetcode