← Visualizers
Sliding Window · sub-variant H — complement / inverse window LeetCode 1423

Maximum Points You Can Obtain from Cards / visualized

You take exactly k cards, each one off the front or the back. That set straddles the two ends of the array, so it is not contiguous and no window can hold it. Its complement can: whatever you leave behind is one unbroken middle block of width exactly n − k. So stop solving the stated problem and solve the inverse — slide a fixed-width window of size n − k, find its minimum sum, and read the answer off as total − minSum. Maximising the ends and minimising the middle are the same instruction seen from opposite sides, which is why the tape below paints both at once. Its partner LC 1658 has the same complement trick with a variable-width middle.

Execution

idle
one picture, two readings  ·  orange ends = cards TAKEN  ·  teal middle = the window you LEAVE
Press Run to begin.
0 / 0
Speed

Java · running line

taken (front + back) the middle window entering the middle leaving the middle minimum middle recorded
O(n) time · O(1) space  ·  one pass for total, one fixed-width slide. Sub-variant H: the asked-for set is split across both ends, so you invert the question and window its complement. answer = total − min(sum of any n−k contiguous cells), and when k == n the window has width 0 and the answer is the whole total.