/** * @param {string}S * @return {number[]} */ var partitionLabels = function (S) { let map = newMap(); for (let i = 0; i < S.length; i++) { if (!map.has(S[i])) map.set(S[i], { start: i }); else map.set(S[i], { start: map.get(S[i]).start, end: i }); } const mapArr = [...map.values()].map((a) => [ a.start, a.end ? a.end : a.start, ]); const res = []; let [min, max] = mapArr[0]; for (let i = 1; i < mapArr.length; i++) { if (mapArr[i][0] > max) { res.push(max - min + 1); min = mapArr[i][0]; max = mapArr[i][1]; } if (mapArr[i][1] > max) max = mapArr[i][1]; } res.push(max - min + 1); return res; };
/** * @param {number[][]}intervals * @return {number[][]} */ var merge = function (intervals) { if (!intervals.length) return []; intervals.sort((a, b) =>Number(a[0]) - Number(b[0])); const res = []; let [min, max] = intervals[0]; for (let i = 1; i < intervals.length; i++) { if (intervals[i][0] > max) { res.push([min, max]); min = intervals[i][0]; max = intervals[i][1]; } if (intervals[i][1] > max) max = intervals[i][1]; } res.push([min, max]); return res; };