`list.windowed(w).map { it.sum() }.max()`
# random
k
list.windowed(w).map { it.sum() }.max()
v
.maxBy {it.sum()}
☝️
k
That doesn't return the actual sum though, only the original list.
v
Right
k
I've been bitten by this a couple of times before too 🙂.
.maxBy {it.sum()}.sum()
🧌
v
their function name
MaximumWindow
is misleading 😠
👍 1
k
"CS people"
v
And now I am stuck thinking which one is actually using the least memory...
k
Yours, mine should have been lazy.
list.toSequence().windowed(w).map{ it.sum() }.max()
would use
O(1)
memory for the max part I think.
v
It allocates at least
w
bytes for windows itself , so it is sub-optimal already
k
True, but here weren't any requirements on the memory consumption of the windowing step.
v
the
O(1)
is a requirement on memory. The optimal hand-written algorithm doesn't need arrays at all