sliding-window · Problem 1 of 2
Maximum Sum of Size K
easy
Amazon
Return the largest sum of any contiguous subarray of exactly length k.
maxSumK([2, 1, 5, 1, 3, 2], 3) -> 9Your solution
Runs your code and animates it without grading anything. Change the input to see what it does on a case the tests do not cover.
Running is free — Submit is what records it. Or press ⌘↩
Tests
4 cases, 1 hidden| call | type | expected | result |
|---|---|---|---|
| maxSumK([2,1,5,1,3,2], 3) | classic | 9 | — |
| maxSumK([1,2,3], 3) | k equals length | 6 | — |
| maxSumK([1,2], 5) | k too large | 0 | — |
| withheld | hidden | withheld | — |
Hidden cases run too — their inputs aren't listed here, so aim for a general solution rather than one fitted to the cases above.
Complexity
- target time
- O(n)
- target space
- O(1)
Fixed-size window. Recomputing each window is O(n·k); sliding reuses all but two elements of the previous sum.
Hints
Stuck? Hints open one at a time, each giving a little more away.
2 hints left