Skip to content

Instantly share code, notes, and snippets.

@Lelith
Created March 12, 2020 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lelith/4af31f0d78a68d915884fb493bf18882 to your computer and use it in GitHub Desktop.
Save Lelith/4af31f0d78a68d915884fb493bf18882 to your computer and use it in GitHub Desktop.
non overlapping segments
function solution(A, B) {
// write your code in JavaScript (Node.js 8.9.4)
if(A.length === 1 ){
return 1;
} else if(A.length ===0){
return 0;
}
let nonOverlapping = 1;
let prevEnd = B[0];
for (let curr = 1; curr < A.length; curr++){
if (A[curr] > prevEnd){
nonOverlapping++;
prevEnd = B[curr];
}
}
return nonOverlapping;
}
try {
const A = [1,3,7,9,9];
const B = [5,6,8,9,10];
console.log(solution(A,B));
}catch(err){console.log(err)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment