Created
December 17, 2019 08:48
-
-
Save iamshadmirza/4fc29a8b9ab16f4e92dcf064db07205e to your computer and use it in GitHub Desktop.
binaryQueue solution from Cassidy's newsletter
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
// This week's question: | |
// Given a positive number N, generate binary numbers between 1 to N | |
// using a queue-type structure in linear time. | |
// Example: | |
// > binaryQueue(10) | |
// > 1 10 11 100 101 110 111 1000 1001 1010 | |
function binaryQueue(n) { | |
const queue = []; | |
const result = []; | |
queue.unshift("1"); | |
while(n > 0){ | |
const current = queue.pop(); | |
result.push(current); | |
queue.unshift(current + "1", current + "0"); | |
n--; | |
} | |
return result; | |
} | |
console.log(binaryQueue(10)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment