-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.2 NailingPlanks.js
51 lines (41 loc) · 1.03 KB
/
14.2 NailingPlanks.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
function getSortedPrefixArr(C, size) {
const r = new Array(size+1).fill(0);
for (let value of C){
r[value] = 1;
}
for (let i = 1; i < r.length; i++){
r[i] += r[i-1];
}
return r;
}
function solution(A, B, C) {
const size = C.length * 2;
if (A.length === 1){
for (let i = 0; i < C.length; i++){
let cvalue = C[i];
if (cvalue >= A[0] && cvalue <= B[0]){
return i+1;
}
}
return -1;
}
let smallerLength = 1;
let biggerLength = C.length;
step: while (true){
let cLength = smallerLength + Math.floor((biggerLength - smallerLength) / 2);
const prefixArr = getSortedPrefixArr(C.slice(0, cLength), size);
for (let i = 0; i < A.length; i++){
if (prefixArr[A[i]-1] >= prefixArr[B[i]]){
if (smallerLength === biggerLength){
return -1;
}
smallerLength = cLength+1;
continue step;
}
}
if (smallerLength === biggerLength){
return biggerLength;
}
biggerLength = cLength;
}
}