Use a monotonic decreasing deque. Store indices, not values. The front always holds the index of the current maximum. When processing index i: First, remove indices from the front that are outside the window (index <i−k+1).
Second, remove indices from the back while nums[back]≤nums[i]. Third, add i to the back. Why remove smaller elements? Because nums[i] is both larger AND enters later. Those smaller elements will never be the maximum while nums[i] is in the window.