Good workout on C# List<int>, and also, experience different style on removing head element if out of sliding window.
3. Blog to read
Method A: naive solution, time complexity O(nw)
Method B: using Self-Balancing Tree (Time complexity: O(nk), need to write c# code)
4. blog:
http://n00tc0d3r.blogspot.ca/2013/04/sliding-window-maximum.html
Good comment about Deque:
the basic insert/delete operations run in constant time.
discussion of using heap:
The first thought might be heap.
By maintaining a heap for all numbers in the window can give us a O(nlogw)-time solution, where
- building up a heap for initial window takes time O(wlogw)
- when window moves to the next number, each insertion and deletion take time O(logw) and there are n-w moves in total.
- after updating the heap, findMax only takes time O(1) since we know the top of heap is the largest.
So, if w << n, the performance of this solution is good, close to O(n); but if w is not that small, say w = n/3 or n/4, the running time goes up to O(nlogn).
know Java Script array very well.