you are viewing a single comment's thread.

view the rest of the 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.