Created
July 6, 2019 06:11
-
-
Save ungarson/740bbb66fb26e9cef27747772a8ec07b to your computer and use it in GitHub Desktop.
find amount of smallest unit length closed intervals in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function findAmountOfSmallestUnitLengthClosedIntervals(sortedPoints) { | |
if(sortedPoints.length <= 1) return 1; | |
let amount = 0; | |
for (let i = 0, len = sortedPoints.length - 1; i < len;) { | |
let unitClosed = false; | |
for (let z = i + 1; z < sortedPoints.length; z++) { | |
if (sortedPoints[z] - sortedPoints[i] < 1) { | |
unitClosed = true; | |
if (z === sortedPoints.length - 1) { | |
i = z; | |
amount += 1; | |
break; | |
} | |
} else if (sortedPoints[z] - sortedPoints[i] === 1) { | |
amount += 1; | |
i = z + 1; | |
break; | |
} else { | |
amount += 1; | |
i = z; | |
if (z === sortedPoints.length - 1) amount += 1; | |
break; | |
} | |
} | |
} | |
return amount; | |
} | |
// Example of usage | |
// const sortedPoints = [ | |
// 1, | |
// 9, | |
// 9.009, | |
// 9.1, | |
// 9.5, | |
// 9.8, | |
// 100 | |
// ] | |
// console.log(findAmountOfSmallestUnitLengthClosedIntervals(sortedPoints)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment