Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Leopoldo Hernandez
We should really be looking at the space we are using here.

Leopoldo Hernandez

Feb 9, 2024

The solution is optimized for time for fails a tad with space. If you notice, no matter how what number we pass, we will always have twice as many numbers in the queue because of the way we are storing this. we can actively guard against this with some conditional clauses.

0

0

Comments
Comments
Leopoldo Hernandez
Leopoldo Hernandez2 years ago
def generateBinaryNumbers(self, n): q = Queue() q.put("1") res = [] while n > 0: res.append(q.get()) if q.qsize() < n: s1 = res[-1] + "0" q.put(s1) if q.qsize() < n: s2 = res[-1] + "1...
Leopoldo Hernandez
Leopoldo Hernandez2 years ago

This helps avoid over enqueing.

R
ray.laiwh a year ago

Agree - I find the solution didn't mention about this part. I am just wondering whether this is the optimised solution in C++ as well. Any thoughts?

This is my code - no controlling on expanding the queue:

#include<queue> #include<iostream> using name...

On this page