Skip to content

Commit c014422

Browse files
committed
4.Non-overlapping Intervals
1 parent 582f532 commit c014422

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @description
3+
* overlapping์ด ์•ˆ๋˜๊ธฐ์œ„ํ•œ ๊ธฐ์ค€์ด ํ•„์š”ํ•จ์„ ๋А๋‚Œ
4+
* ์ฒ˜์Œ์—๋Š” ์‹œ์ž‘์ , ๋์ ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ–ˆ์ง€๋งŒ 16๋ฒˆ ํ…Œ์ŠคํŠธ์—์„œ ์‹คํŒจ
5+
* ์ •๋ ฌ๊ธฐ์ค€์ด ๋์ , ์‹œ์ž‘์  ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์•ผํ•œ๋‹ค๊ณ  ๊นจ๋‹ซ๊ฒŒ ๋จ
6+
*
7+
* n = length of intervals
8+
* time complexity: O(n log n)
9+
* space complexity: O(n)
10+
*/
11+
var eraseOverlapIntervals = function (intervals) {
12+
intervals.sort((a, b) => {
13+
if (a[1] !== b[1]) return a[1] - b[1];
14+
if (a[0] !== b[0]) return b[0] - a[0];
15+
return 0;
16+
});
17+
18+
let answer = 0;
19+
const current = intervals[0];
20+
21+
for (let i = 1; i < intervals.length; i++) {
22+
const [start, end] = intervals[i];
23+
24+
if (current[1] > start) answer++;
25+
else {
26+
current[0] = start;
27+
current[1] = end;
28+
}
29+
}
30+
31+
return answer;
32+
};

0 commit comments

Comments
ย (0)