Quant Memo
Coding/●●●●

Two non-overlapping windows of fixed length

Asked at Millennium, Two Sigma

You have a series of daily desk revenues. Choose two non-overlapping contiguous windows, one of length L and one of length M, to maximize their combined sum. The L window may come before or after the M window.

rev = [0, 6, 5, 2, 2, 5, 1, 9, 4], L = 1, M = 2
-> 20          # length-2 window [6, 5] = 11, length-1 window [9] = 9

Return the maximum combined sum in O(n)O(n).

Show a hint

Fix which window comes second and slide its right end across the array. As you go, keep the best first window that ends before the second window starts. Then do it again with the roles swapped.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions