-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_solution.js
99 lines (92 loc) · 2.35 KB
/
my_solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* @param {number[][]} intervals
* @return {number}
*/
// const eraseOverlapIntervals = (intervals) => {
// let count = 0;
// if (intervals.length <= 1) return count;
//
// intervals.sort((a, b) => a[0] - b[0]);
//
// let prevEnd = Number.NEGATIVE_INFINITY;
// while (intervals.length > 0) {
// let curr = intervals.shift();
//
// // if (curr[0] < prev[1] && prev[1] > curr[1]) {
// // count++;
// // prev = curr; // replace because THIS is can be removed.
// // } else if (curr[0] < prev[1] && prev[1] <= curr[1]) {
// // count++;
// // // not replace bc im going to remove the current, so prev stays.
// // } else {
// // prev = curr;
// // }
//
// if (curr[0] >= prevEnd) {
// prevEnd = curr[0]; // replace because THIS is can be removed.
// } else {
// count++;
// }
// }
//
// return count;
// }
/**
* @param {number[][]} intervals
* @return {number}
*/
const eraseOverlapIntervals = (intervals) => {
let count = 0;
// exit
if (intervals.length <= 1) return count;
intervals.sort((a, b) => a[0] - b[0]);
console.log(intervals);
// while intervals.length > 0
let prev = intervals.shift();
while (intervals.length > 0) {
let curr = intervals.shift();
if (curr[0] < prev[1] && prev[1] > curr[1]) {
count++;
prev = curr;
} else
if (curr[0] < prev[1] && prev[1] <= curr[1]) {
count++;
} else {
prev = curr;
}
}
// if (curr[0] < prev[1] && prev[1] < curr[1]) count ++;
return count;
}
// /**
// * @param {number[][]} intervals
// * @return {number}
// */
// const eraseOverlapIntervals = (intervals) => {
// let count = 0;
// // exit
// if (intervals.length <= 1) return count;
//
// intervals.sort((a, b) => a[0] - b[0]);
//
// console.log(intervals);
// // while intervals.length > 0
// let prev = intervals.shift();
// while (intervals.length > 0) {
// let curr = intervals.shift();
//
// if (curr[0] < prev[1] && prev[1] > curr[1]) {
// count++;
// prev = curr;
// } else
// if (curr[0] < prev[1] && prev[1] <= curr[1]) {
// count++;
// } else {
// prev = curr;
// }
//
// }
//
// // if (curr[0] < prev[1] && prev[1] < curr[1]) count ++;
// return count;
// }