Nuggets team number online, help you Offer impromptu! Click for details

I. Title Description:

The array enjoyments is a set of intervals, where a single interval is enjoyments [I] = [Starti, endi]. You merge all overlapping ranges and return a non-overlapping array of ranges that exactly covers all of the ranges in the input.

Example 1:

Input: intervals = [[1,3],[2,6],[8,10],[15,18]]

Output: [[1, 6], [8, 10], [15, 17]]

Interpretation: the intervals [1,3] and [2,6] overlap, merging them into [1,6].

Example 2:

Input: intervals = [[1,4],[4,5]]

Output: [[1, 5]]

Explanation: the intervals [1,4] and [4,5] can be regarded as overlapping intervals.

Tip:

  • 1 <= intervals.length <= 104
  • intervals[i].length == 2
  • 0 <= starti <= endi <= 104

Title link: leetcode-cn.com/problems/me…

Ii. Analysis of Ideas:

First you need the reading group to sort (in ascending order)

intervals.sort(function(a,b){
    return a[0]-b[0]
  })

Copy the code

Then, define a new array to store the new merge interval, compare the original array with the last item of the newly defined array, the left boundary of the current item is larger than the right boundary of the latter item, that is, there is intersection, just change the right boundary of the latter item into the right boundary of the former item

The execution result fails. In this case, the latter item may contain the former item, namely [1,4],[2,3]. In accordance with the above logic, it is wrong to judge the combination result as [1,3]

Therefore, it is necessary to continue to judge before merging. If the left boundary of the former term is greater than > = the left boundary of the latter term, it will not be skipped; otherwise, it will be merged according to the above ideas

Iii. AC Code:

var merge = function(intervals) {
    let newArr = []
    intervals.sort((a,b) => {
        return a[0] - b[0]
    })
    newArr.push(intervals[0])
    for(let i = 1; i < intervals.length; i++) {
        let len = newArr.length;
        let cur = intervals[i];
        let pre = newArr[len-1]
        if(cur[0] > pre[1]){
            newArr.push(cur)
        }else{
            if(cur[1] > pre[1]){
                newArr[len-1][1] = cur[1]
            }
        }
    }
    return newArr
};
Copy the code

Iv. Summary:

The point of this problem is to sort it first, and then loop through the comparison, which makes it easier.

Return a[0] -b [0] return a[0] -b [0] return a[0] -b [0] return a[0] -B [0] So a[0] -b [0], the comparison function returns a value less than 0; If you want to sort in descending order, then a[0] comes after, and the comparison function should return a value greater than 0, so b[0] -a [0]

intervals.sort((a,b) => {
        return a[0] - b[0]
    })
Copy the code

I did not think of a good way to solve the beginning, read the solution to have some ideas, welcome criticism.

This article is participating in the nuggets team number online activity, click to see the dachang spring recruiting positions