We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Both the one-line Python3 solutions of metazilla and it_henrik are beautiful. My independent two-liner also doesn't assume that c is sorted. My code's advantages:
no use of enumerate (and indeed a generator is used in the outer sum)
roughly n//k * 3 multiplications/divisions versus n * 2.
If one wanted to get it down to n//k multiplications, one would insert a line that creates a memo-ization array of the start/stop indices r*k from 0 to n by incrementing by k.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Greedy Florist
You are viewing a single comment's thread. Return to all comments →
Both the one-line Python3 solutions of
metazilla
andit_henrik
are beautiful. My independent two-liner also doesn't assume thatc
is sorted. My code's advantages:enumerate
(and indeed a generator is used in the outer sum)n//k * 3
multiplications/divisions versusn * 2
.If one wanted to get it down to
n//k
multiplications, one would insert a line that creates a memo-ization array of the start/stop indicesr*k
from0
ton
by incrementing byk
.