← 返回 citadel 的题目列表Top K Elements in Each Sliding Window
类型:online_judge
Problem
Given an integer array nums, a window size w, and an integer k, slide the window from left to right by one position each time. For every window of length w, return the largest k elements inside that window.
Requirements:
Output exactly k elements for each window.
Elements should be printed in descending order.
Duplicates must be preserved. For example, the Top 2 elements of [4, 4, 2] are [4, 4].
Input Format
n w k
nums[0] nums[1] ... nums[n-1]
Output Format
Print n - w + 1 lines. The i-th line contains the Top k elements of the i-th window in descending order, separated by spaces.
Constraints
1 <= n <= 2 * 10^5
1 <= k <= w <= n
-10^9 <= nums[i] <= 10^9
The output size is O((n - w + 1) * k), so the algorithm should be output-sensitive
Example
Input:
5 3 2
1 3 2 5 4
Output:
3 2
5 3
5 4
Example
Input
5 3 2
1 3 2 5 4
Output
3 2
5 3
5 4