The positive-sum sliding window returns the shortest window full
This proof shows: correctness.
We use the pseudocode of the page, on A[0..n−1] with A[i]≥0 for all i and s>0. Write S(i,j)=A[i]+⋯+A[j] for i≤j and S(i,i−1)=0. For 0≤i≤n−1 let e(i) be the smallest j with i≤j≤n−1 and S(i,j)≥s, or ∞ if there is none. Let D=mini(e(i)−i+1), with D=∞ if every e(i) is ∞. The answer is D, or 0 when D=∞.
Lemma 1 (monotone sums). If i≤i′ and j′≤j, then S(i′,j′)≤S(i,j).
Proof. S(i,j)−S(i′,j′) is a sum of items, so it is not negative. ■
Lemma 2 (inner loop). Suppose that before line L4 of step r we have t=S(l,r) and S(l,r−1)<s, with l≤r. Then the inner loop stops. Each time line L5 runs, e(l)=r for the current l, and it records r−l+1=e(l)−l+1. When the loop stops, t=S(l,r)<s and l≤r+1.
Proof. The loop keeps t=S(l,r), since line L6 removes A[l] and then moves l. When l=r+1, t=S(r+1,r)=0<s, so the loop runs at most r−l+1 times. Each time the test t≥s passes with l≤r, we have S(l,r)≥s. Also S(l,r−1)<s: for the first pass this is the hypothesis, and for a later pass with a larger l it follows from Lemma 1 and the hypothesis on the first l. So r is the least end with sum at least s from start l, that is, e(l)=r. ■
Claim. At the start of step r, for 0≤r≤n (with step n meaning the moment the outer loop ends):
- 0≤l≤r and t=S(l,r−1)<s;
- for every i<l, e(i)≤r−1 and best≤e(i)−i+1;
- if best<∞, then best=e(i)−i+1 for some i<l.
Proof. By induction on r.
Base, r=0. Line L1 sets l=0, t=0=S(0,−1)<s and best=∞. Parts 2 and 3 hold, because there is no i<0 and best=∞.
Step. Assume the claim at the start of step r<n. Line L3 sets t=S(l,r), and part 1 gives S(l,r−1)<s. Let l0 be l at this moment and l1 be l after the inner loop. By Lemma 2 the inner loop stops with t=S(l1,r)<s and l1≤r+1, so part 1 holds at the start of step r+1.
The starts i with l0≤i<l1 are those that the inner loop drops. For each of them Lemma 2 gives e(i)=r, and line L5 makes best≤r−i+1=e(i)−i+1. For i<l0, part 2 gave e(i)≤r−1 and best≤e(i)−i+1, and best only decreases. So part 2 holds for every i<l1 at the start of step r+1. Every value that line L5 writes is e(i)−i+1 for a dropped start i, which gives part 3. ■
Theorem. MIN-WINDOW returns D if D<∞, and 0 otherwise.
Proof. Apply the claim at r=n. For i<l, part 2 gives best≤e(i)−i+1. For i≥l, Lemma 1 and part 1 give S(i,n−1)≤S(l,n−1)=t<s, so S(i,j)<s for every j≥i and e(i)=∞. So best≤D. Part 3 gives best≥D when best is finite, and if best=∞ then no start has a finite e(i), by part 2 applied to i<l, so D=∞. So best=D, and lines L7 and L8 return the answer.
Termination and cost. The outer loop runs n times. Line L6 increases l, and l≤r+1≤n by part 1, so line L6 runs at most n times in all, and line L4 fails exactly n times. The algorithm stops after O(n) operations. ■