-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxNonoverlapping.js
52 lines (44 loc) · 1.23 KB
/
maxNonoverlapping.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
// -----------------------------------------------------------------------------
// MAX NONOVERLAPPING SEGMENTS
// Find a maximal set of non-overlapping segments.
//
// Report: https://app.codility.com/demo/results/trainingSMUCGV-KYM/
// -----------------------------------------------------------------------------
function solution(A, B) {
if (A.length === 0) {
return 0
}
let count = 1
let i = 0
let j = i + 1
while (i < A.length && j < A.length) {
const overlap = A[j] <= B[i]
if (!overlap) {
// console.log(`count segment ${A[i]}--${B[i]}, ${A[j]}--${B[j]}`,)
count += 1
i = j
}
j += 1
}
return count
}
console.log(solution([1, 3, 7, 9, 9], [5, 6, 8, 9, 10])) // 3
// 1 2 3 4 5 6 7 8 9 10
// x-------x
// x-----x
// x-x
// x
// x-x
console.log(solution([], [])) // 0
console.log(solution([0], [0])) // 1
console.log(solution([0, 2, 100], [0, 50, 1000])) // 3
console.log(solution([1, 3, 5], [4, 6, 8])) // 2
// 1 2 3 4 5 6 7 8
// x-----x
// x-----x
// x-----x
console.log(solution([1, 5, 3], [4, 7, 8])) // 2
// 1 2 3 4 5 6 7 8
// x-----x
// x---x
// x---------x