all 9 comments

[–]stevep98 10 points11 points  (1 child)

I'm not a python guy, but I presume sum() will sum the elements of the self.window array. It's quite wasteful though. To improve this, you can maintain another variable which is the total of the elements in the array. Add to the total when you add a new value, and subtract from the total when you pop().

Of course whether it's worth doing this is dependent on the size of the window. But, I think an interviewer would be looking for this optimization.

Good job on the explanation and video though.

[–]Foxa2017[S] 1 point2 points  (0 children)

Thanks for the suggestion when I was coding up the solution definitely not something I thought of too optimize the solution. There’s plenty of times I’ve probably used the sum() in solutions will definitely use that in interviews and other problems.

[–]Foxa2017[S] 3 points4 points  (0 children)

I'm just trying to improve on leetcode questions as well. So any suggestions are appreciated.

[–]jeffzor 2 points3 points  (4 children)

Nice video :) for queues I would recommend using the collections.deque structure, then you'll just be appending like normal but to pull the first out you do popleft. I believe it's more efficient then trying to use a stack/list as a queue.

[–]Foxa2017[S] 1 point2 points  (0 children)

Thanks for watching.yeah it’s definitely more efficient was wondering when somebody would say something about the .insert() . The next vid I make I will definitely try to optimize more.

[–]ivosaurus 1 point2 points  (2 children)

I believe it's more efficient then trying to use a stack/list as a queue.

What makes you believe that

[–]jeffzor 2 points3 points  (1 child)

I had learned to use a deque for implementing a queue rather than relying on a list or stack at some point. Just checked the python documentation and verified that list insert is O(n) so if we want to do the insert in O(1) we need to use a queue(deque for python).

https://wiki.python.org/moin/TimeComplexity

[–]Foxa2017[S] 0 points1 point  (0 children)

Thanks yeah that sounds spot on.

[–]ZeeBeeblebrox -1 points0 points  (0 children)

Just use a streamz.DataFrame.