-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxNonoverlappingSegments.js
38 lines (31 loc) · 1 KB
/
maxNonoverlappingSegments.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
const maxNonoverlappingSegments = (A, B) => {
const N = A.length;
if (N === 0) {
return 0;
}
let count = N;
let right = B[0];
for (let i = 0; i < N - 1; i++) {
if (A[i + 1] <= right) {
count--;
} else {
right = B[i + 1];
}
}
// let count = 1;
// let right = B[0];
// for (let i = 1; i < N; i++) {
// if (A[i] > right) {
// count++;
// right = B[i];
// }
// }
return count;
};
console.log(maxNonoverlappingSegments([1, 3, 7, 9, 9], [5, 6, 8, 9, 10])); // 3
console.log(maxNonoverlappingSegments([1, 3, 7, 19, 19], [2, 6, 8, 19, 20])); // 4
console.log(maxNonoverlappingSegments([1, 3, 7, 19, 20], [2, 6, 8, 19, 20])); // 5
console.log(maxNonoverlappingSegments([1, 3, 7, 19, 20], [3, 6, 8, 19, 20])); // 4
console.log(maxNonoverlappingSegments([1, 3, 7, 19, 20], [3, 3, 19, 19, 20])); // 3
console.log(maxNonoverlappingSegments([1, 1, 1, 1, 1], [1, 1, 1, 1, 1])); // 1
console.log(maxNonoverlappingSegments([0, 2, 6, 8, 8], [5, 6, 8, 9, 10])); // 2