← 返回 roblox 的题目列表Sliding Windows Containing a Target; Earliest Window with Max Target Frequency
类型:online_judge
Coding: Sliding Windows Containing a Target + Earliest Window with Maximum Target Count
Given an integer array nums, an integer win_size (window length), and an integer target.
A window is any contiguous subarray of nums with length exactly win_size (windows may overlap).
Complete two tasks:
(1) Find all windows that contain target
Return the list of start indices starts (left to right) such that the window nums[i : i+win_size] contains target at least once.
(2) Find the earliest window with the maximum target frequency
Among all windows of length win_size, find the window(s) with the largest number of occurrences of target. If there is a tie, return the earliest such window's start index.
If win_size > len(nums), then:
(1) return an empty list
(2) return -1
Input (stdin)
Three lines:
nums: space-separated integers
win_size: integer
target: integer
Output (stdout)
Two lines:
Start indices for (1), space-separated (print an empty line if none)
The start index for (2) (print -1 if no window exists)
Constraints
1 <= len(nums) <= 2e5
-1e9 <= nums[i], target <= 1e9
1 <= win_size <= 2e5
Example
Input
1 2 3 2 2 4
3
2
Output
0 1 2 3
1
Explanation:
Windows of length 3 start at indices 0..3.
Every window contains at least one 2, so (1) prints 0 1 2 3.
Target counts per window are [1,1,2,2]; max is 2, earliest start is 1, so (2) prints 1.
Example
Input
1 2 3 2 2 4
3
2
Output
0 1 2 3
1