You are given two non-empty arrays A and B consisting of N integers. These arrays represent N planks. More precisely, A[K] is the start and B[K] the end of the K−th plank.
Next, you are given a non-empty array C consisting of M integers. This array represents M nails. More precisely, C[I] is the position where you can hammer in the I−th nail.
We say that a plank (A[K], B[K]) is nailed if there exists a nail C[I] such that A[K] ≤ C[I] ≤ B[K].
The goal is to find the minimum number of nails that must be used until all the planks are nailed. In other words, you should find a value J such that all planks will be nailed after using only the first J nails. More precisely, for every plank (A[K], B[K]) such that 0 ≤ K < N, there should exist a nail C[I] such that I < J and A[K] ≤ C[I] ≤ B[K].
For example, given arrays A, B such that:
A[0] = 1 B[0] = 4 A[1] = 4 B[1] = 5 A[2] = 5 B[2] = 9 A[3] = 8 B[3] = 10four planks are represented: [1, 4], [4, 5], [5, 9] and [8, 10].
Given array C such that:
C[0] = 4 C[1] = 6 C[2] = 7 C[3] = 10 C[4] = 2if we use the following nails:
- 0, then planks [1, 4] and [4, 5] will both be nailed.
- 0, 1, then planks [1, 4], [4, 5] and [5, 9] will be nailed.
- 0, 1, 2, then planks [1, 4], [4, 5] and [5, 9] will be nailed.
- 0, 1, 2, 3, then all the planks will be nailed.
Thus, four is the minimum number of nails that, used sequentially, allow all the planks to be nailed.
Write a function:
function solution(A, B, C);
that, given two non-empty arrays A and B consisting of N integers and a non-empty array C consisting of M integers, returns the minimum number of nails that, used sequentially, allow all the planks to be nailed.
If it is not possible to nail all the planks, the function should return −1.
For example, given arrays A, B, C such that:
A[0] = 1 B[0] = 4 A[1] = 4 B[1] = 5 A[2] = 5 B[2] = 9 A[3] = 8 B[3] = 10 C[0] = 4 C[1] = 6 C[2] = 7 C[3] = 10 C[4] = 2the function should return 4, as explained above.
Write an efficient algorithm for the following assumptions:
- N and M are integers within the range [1..30,000];
- each element of arrays A, B and C is an integer within the range [1..2*M];
- A[K] ≤ B[K].
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
[1], [1], [2]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
[1] [1] [2]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
[[1, 1, 2], [], []]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
[1], [1], [2]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
[[1], [], []]
[[1], [], []]
[[2], [], []]
function result: -1
function result: -1
function result: -1
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
(1) (1) (2)
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
[[1], [1], [2]]
TypeError: Cannot read property 'start' of undefined at solution (solution.js:22:29) at solutionWrapper (/tmp/exec.js:422:28) at Promise.resolve.then (/tmp/exec.js:448:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
console.log('Nail: ' + C[i])
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
console.log(mid);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
console.log('Nail: ' + C[i])
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
console.log('Mid: ' + mid);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
[[1], [1], [2]]
Nail: 4 Mid: 2 Mid: 0 Mid: 0 Nail: 6 Mid: 1 Mid: 0 Nail: 7 Mid: 0 Nail: 10 Mid: 0
TypeError: Cannot read property 'start' of undefined at solution (solution.js:23:29) at solutionWrapper (/tmp/exec.js:422:28) at Promise.resolve.then (/tmp/exec.js:448:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3stdout:
Nail: 2 Mid: 0 Mid: 1
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
console.log('Nail: ' + C[i])
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
console.log('Mid: ' + mid);
console.log(planks)
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
[[1], [1], [2]]
Nail: 4 Mid: 2 [ { start: 1, end: 4 }, { start: 4, end: 5 }, { start: 5, end: 9 }, { start: 8, end: 10 } ] Mid: 0 [ { start: 1, end: 4 }, { start: 4, end: 5 }, { start: 5, end: 9 }, { start: 8, end: 10 } ] Mid: 0 [ { start: 4, end: 5 }, { start: 5, end: 9 }, { start: 8, end: 10 } ] Nail: 6 Mid: 1 [ { start: 5, end: 9 }, { start: 8, end: 10 } ] Mid: 0 [ { start: 5, end: 9 }, { start: 8, end: 10 } ] Nail: 7 Mid: 0 [ { start: 8, end: 10 } ] Nail: 10 Mid: 0 [ { start: 8, end: 10 } ]
TypeError: Cannot read property 'start' of undefined at solution (solution.js:24:29) at solutionWrapper (/tmp/exec.js:422:28) at Promise.resolve.then (/tmp/exec.js:448:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3stdout:
Nail: 2 Mid: 0 [ { start: 1, end: 1 } ] Mid: 1 [ { start: 1, end: 1 } ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
console.log('Nail: ' + C[i])
let begin = 0;
let end = planks.length;
while (begin < end) {
const mid = Math.floor((begin + end) / 2);
console.log('Mid: ' + mid);
console.log(planks)
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
console.log('Nail: ' + C[i])
let begin = 0;
let end = planks.length;
while (begin < end) {
const mid = Math.floor((begin + end) / 2);
console.log('Mid: ' + mid);
console.log(planks)
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
[[1], [1], [2]]
Nail: 4 Mid: 2 [ { start: 1, end: 4 }, { start: 4, end: 5 }, { start: 5, end: 9 }, { start: 8, end: 10 } ] Mid: 0 [ { start: 1, end: 4 }, { start: 4, end: 5 }, { start: 5, end: 9 }, { start: 8, end: 10 } ] Nail: 6 Mid: 1 [ { start: 4, end: 5 }, { start: 5, end: 9 }, { start: 8, end: 10 } ] Mid: 1 [ { start: 4, end: 5 }, { start: 8, end: 10 } ] Nail: 7 Mid: 1 [ { start: 4, end: 5 }, { start: 8, end: 10 } ] Nail: 10 Mid: 1 [ { start: 4, end: 5 }, { start: 8, end: 10 } ] Mid: 0 [ { start: 4, end: 5 } ] Nail: 2 Mid: 0 [ { start: 4, end: 5 } ]
function result: -1
Nail: 2 Mid: 0 [ { start: 1, end: 1 } ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
console.log('Nail: ' + C[i])
let begin = 0;
let end = planks.length;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
console.log('Mid: ' + mid);
console.log(planks)
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
console.log('Nail: ' + C[i])
let begin = 0;
let end = planks.length - 1;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
console.log('Mid: ' + mid);
console.log(planks)
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
[[1], [1], [2]]
Nail: 4 Mid: 1 [ { start: 1, end: 4 }, { start: 4, end: 5 }, { start: 5, end: 9 }, { start: 8, end: 10 } ] Mid: 1 [ { start: 1, end: 4 }, { start: 5, end: 9 }, { start: 8, end: 10 } ] Mid: 0 [ { start: 1, end: 4 }, { start: 5, end: 9 }, { start: 8, end: 10 } ] Nail: 6 Mid: 0 [ { start: 5, end: 9 }, { start: 8, end: 10 } ] Mid: 0 [ { start: 8, end: 10 } ] Nail: 7 Mid: 0 [ { start: 8, end: 10 } ] Nail: 10 Mid: 0 [ { start: 8, end: 10 } ]
function result: -1
Nail: 2 Mid: 0 [ { start: 1, end: 1 } ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length - 1;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
[[1], [1], [2]]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length - 1;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
[[1], [1], [2]]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B, C) {
const N = A.length;
const M = C.length;
const planks = [];
for (let i = 0; i < N; i++) {
planks.push({ start: A[i], end: B[i] });
}
planks.sort((a, b) => a.start - b.start);
for(let i = 0; i < M; i++) {
let begin = 0;
let end = planks.length - 1;
while (begin <= end) {
const mid = Math.floor((begin + end) / 2);
if (planks[mid].start > C[i]) {
end = mid - 1;
} else if (planks[mid].end < C[i]) {
begin = mid + 1
} else {
planks.splice(mid, 1);
end--;
if (planks.length === 0) {
return i+1;
}
}
}
}
return -1;
}
The solution obtained perfect score.