Ask for help to design an algorithm, but you can't figure out how to achieve it. How to write this in code?

write a merge function

[
    { start: 1, end: 2 },
        { start: 2, end: 3 },
        { start: 5, end: 6 },
        { start: 3, end: 5 },
        { start: 8, end: 9 },
        
    
]
The

parameter is an array

if the parameters only have {start: 1, end: 2},
{start: 2, end: 3} merge into {start: 1, end: 3} and then return the array [{start: 1, end: 3}]

.

if parameters {start: 1, end: 2},
{start: 2, end: 3} {start: 5, end: 6}

return the array [{start: 1, end: 3}, {start: 5, end: 6}]

at this time.

if the parameter is

[
    { start: 1, end: 2 },
        { start: 2, end: 3 },
        { start: 5, end: 6 },
        { start: 3, end: 5 },
        { start: 8, end: 9 },

        { start: 11, end: 15 },

    ]

return [{start: 1, end: 6}, {start: 8, end: 9}, {start: 11, end: 15}]

Jul.15,2021

talk about clapping your head and thinking of the best way to understand, there must be a better solution.
define an array.
let mark =

the initial length is undetermined.
starts traversing the incoming array. If the first object is s1e5, first complete the mark to 5 digits, and then change the first to the fifth position of mark to 1. Become
[1 code >
]

and so on
the final mark array is an array. composed of 0 and 1
to judge the starting and ending positions of consecutive 1s on this array, and to form the result set.


leetcode almost has the original title, address

Code https://github.com/hanzichi/l. can refer to


the easiest thing to do is to sort each object in the array by start from smallest to largest, and then traverse whether to merge


let arr = [
    { start: 1, end: 2 },
    { start: 2, end: 3 },
    { start: 5, end: 6 },
    { start: 3, end: 5 },
    { start: 8, end: 9 },
    { start: 11, end: 15 },

]

let res = arr.sort((acc, cur) => acc.start - cur.start).reduce((acc, cur) => {
    if (acc) {
        let last = acc[acc.length - 1];
        if (last.end === cur.start) {
            last.end = cur.end;
        } else {
            acc.push(cur)
        }
        return acc;
    }
    return [cur];
}, null)

let data = [
    { start: 1, end: 2 },
    { start: 2, end: 3 },
    { start: 5, end: 6 },
    { start: 3, end: 5 },
    { start: 8, end: 9 },
    { start: 11, end: 15 }
]
let result = []
data.sort((a,b) => {
    return a.start - b.start
}).map( val => {
    let temp = result[result.length-1]
    result.length && val.start == temp.end ? temp.end = val.end : result.push(val)
    return val
})
console.log(result)

using the method of mark array, if there are fewer data flags, it will take up too much space, which is definitely not suitable
. I think the reasonable solution to this problem is whether recursive scans are merged (preferably sorted first)
the approximate algorithm is as follows

1.start0

2.1

3.1
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e48bb9-44519.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e48bb9-44519.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?