Skip to content

Commit 09debd2

Browse files
committed
merge-intervals refactor by allocationg often referred array to a variable
1 parent 0518c0a commit 09debd2

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

merge-intervals/jdy8739.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@
55
var merge = function (intervals) {
66
const sort = intervals.sort((a, b) => a[0] - b[0]);
77

8-
const arr = [sort[0]];
8+
const mergedIntervals = [sort[0]];
99

1010
for (let i = 1; i < sort.length; i++) {
11-
const endOfArr = arr[arr.length - 1][1];
11+
/** 현재 합쳐진 인터벌의 마지막 요소 */
12+
const lastMergedInterval = mergedIntervals[mergedIntervals.length - 1];
13+
14+
const endOfMergedInterval = lastMergedInterval[1];
1215

1316
const next = sort[i][0];
1417

15-
if (endOfArr < next) {
16-
arr.push(sort[i]);
18+
if (endOfMergedInterval < next) {
19+
mergedIntervals.push(sort[i]);
1720
} else {
18-
arr[arr.length - 1][1] = Math.max(arr[arr.length - 1][1], sort[i][1]);
21+
lastMergedInterval[1] = Math.max(lastMergedInterval[1], sort[i][1]);
1922
}
2023
}
2124

22-
return arr;
25+
return mergedIntervals;
2326
};
2427

2528
// 시간복잡도 O(nlogn) -> sort 함수의 시간복잡도가 O(nlogn)이기 때문에

0 commit comments

Comments
 (0)