We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7e33d11 commit b82ace8Copy full SHA for b82ace8
1 file changed
non-overlapping-intervals/byol-han.js
@@ -0,0 +1,30 @@
1
+/**
2
+ * https://leetcode.com/problems/non-overlapping-intervals/description/
3
+ * @param {number[][]} intervals
4
+ * @return {number}
5
+ */
6
+var eraseOverlapIntervals = function (intervals) {
7
+ if (intervals.length === 0) return 0;
8
+
9
+ // 1. intervals를 end 값을 기준으로 오름차순 정렬
10
+ intervals.sort((a, b) => a[1] - b[1]);
11
12
+ // 2. 첫 번째 interval의 end 값으로 초기화
13
+ let end = intervals[0][1];
14
+ let count = 0; // 제거해야 하는 interval의 개수
15
16
+ // 3. 두 번째 interval부터 순회
17
+ for (let i = 1; i < intervals.length; i++) {
18
+ let [start_i, end_i] = intervals[i];
19
20
+ if (start_i < end) {
21
+ // 현재 interval의 start가 이전 end보다 작으면 overlap
22
+ count++; // 이 interval은 제거해야 함
23
+ } else {
24
+ // overlap이 없으면 end 갱신
25
+ end = end_i;
26
+ }
27
28
29
+ return count;
30
+};
0 commit comments