LeetCode 239 Sliding Window Maximum - Why Naive Fails

The trap

For each window position, scan all k elements to find the maximum.

There are nk+1n - k + 1 windows. Scanning each takes O(k)O(k). Total: O(nk)O(nk).

For n=105n = 10^5 and k=104k = 10^4, that's 10910^9 operations. Too slow.