visual walkthrough
Maximum Points You Can Obtain from Cards
Maximise the ends by minimising the middle window, a neat reframing.
The idea
You must take k cards, always from the two ends. So whatever you leave behind is one contiguous block of n − k cards in the middle.
Maximising the points you take is the same as minimising the sum of that middle block, which is a fixed-size sliding window.
Complexity
| approach | time | space |
|---|---|---|
| Try every split | O(k²) | O(1) |
| Smallest middle block | O(n) | O(1) |