This is usually caught by people. You might think that it will take O(N^2)time, since you add, it takes time O(N), and you have the elements O(N). However, each element can be implemented only once and deleted once. Thus, a total of gliding over the entire array Ais required O(N).
This gives amortized efficiency O(1)every time you move a sliding window one element. In other words, the average time required to move the sliding window by one element is O(1).
source
share