· worked example · written by the site with AI help
When does a sliding window stop working?
Task: longest contiguous block with sum at most 8. For [3, 1, 2, 4, 1] the two-pointer window gives 4 (3, 1, 2 sums to 6; 1, 2, 4, 1 sums to 8). It works because adding an element never lowers the sum. Put a negative number in, such as [9, -5, 1], and shrinking too early throws away an answer: the whole array sums to 5.
Write the sentence that must be true about the data before you are allowed to move the left pointer.滑动窗口的前提是什么?给一个它失效的例子。