You are viewing a single comment's thread. Return to all comments →
There is nothing to beat in your code bro :D
You could use a generator expression instead of a list comprehension.
def getWays(squares, d, m):
return sum(1 for i in range(n-m+1) if sum(s[i:i+m])==d)
But if you want a more efficient solution for larger inputs, use a sliding window (see the following link). https://www.hackerrank.com/challenges/the-birthday-bar/submissions/code/43358272
By "sliding window" don't you mean a loop whithin a loop, so that it counts a surden amount ahead for each item in the lst.
By "sliding window", I mean one loop without nesting another loop within it. When you advance to the next item, you add one item and subtract another item.
Yo, thanks for that description. I hadn't thought of that. Made the solution so much cleaner.
can u explain ur code pls!!!!!
There's nothing to explain; it's all obvious.
return sum(sum(s[element:element+m) == d for element in range(len(s))])