Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-332

Sliding Window Deque Moves

Problem

Process every size-k window with a monotonic deque of indices and count its operations. back_pops counts removals from the back while the back value is <= the incoming value; front_pops counts removals from the front when the front index has left the window. Return [back_pops, front_pops], or [] when k is not positive or larger than the array.

Hint

Values leave the back when a larger newcomer arrives and the front when their index is out of range.

Test cases

case 1
in: [[1,3,2,5,4],3]
expected: [3,0]
case 2
in: [[5,5,5,5],2]
expected: [3,0]
case 3
in: [[1],1]
expected: [0,0]
case 4
in: [[1,2],3]
expected: []
Starter codePython
def sliding_window_deque_moves(nums, k):
    # Your code here
    pass

4 test cases · run your code against them in the editor