문제
Slide a window of size k across the list one step at a time and report the maximum inside each window. A deque keeps this O(n).
Implement window_maximums(numbers, k).
Input
numbers: The list of integers. k: The window size.
Output
Return the maximum of each window, left to right.
제약 조건
1 ≤ k ≤ length ≤ 200,000
예시
입력:
numbers = [1, 3, -1, -3, 5, 3, 6, 7]
k = 3
예상 출력:
expected = [3, 3, 5, 5, 6, 7]
입력:
std::vector<long long> numbers = {1, 3, -1, -3, 5, 3, 6, 7};
long long k = 3;
예상 출력:
std::vector<long long> expected = {3, 3, 5, 5, 6, 7};
입력:
long[] numbers = new long[] {1, 3, -1, -3, 5, 3, 6, 7};
long k = 3;
예상 출력:
long[] expected = new long[] {3, 3, 5, 5, 6, 7};
설명
Windows [1,3,-1], [3,-1,-3], [-1,-3,5], … have maxima 3, 3, 5, 5, 6, 7.
입력:
numbers = [4, 2]
k = 1
예상 출력:
expected = [4, 2]
입력:
std::vector<long long> numbers = {4, 2};
long long k = 1;
예상 출력:
std::vector<long long> expected = {4, 2};
입력:
long[] numbers = new long[] {4, 2};
long k = 1;
예상 출력:
long[] expected = new long[] {4, 2};
설명
Windows of one element are the elements themselves: [4, 2].
접근 방법
먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.
힌트 1
The maximum can disappear when it leaves the window, so you need a structure that updates by both value and index.
힌트 2
Keep candidate indices in decreasing order of their values, dropping anything outside the current window.
힌트 3
Watch the first full window and windows of size one; every answer must correspond to a fully formed window.
예상 복잡도
O(n) time, O(k) extra space
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제약 조건을 기준으로 풀어 주세요.