Đề bài
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.
Ràng buộc
1 ≤ k ≤ length ≤ 200,000
Ví dụ
Đầu vào:
k = 3
numbers = [1, 3, -1, -3, 5, 3, 6, 7]
Kết quả mong đợi:
expected = [3, 3, 5, 5, 6, 7]
Đầu vào:
long long k = 3;
std::vector<long long> numbers = {1, 3, -1, -3, 5, 3, 6, 7};
Kết quả mong đợi:
std::vector<long long> expected = {3, 3, 5, 5, 6, 7};
Đầu vào:
long k = 3;
long[] numbers = new long[] {1, 3, -1, -3, 5, 3, 6, 7};
Kết quả mong đợi:
long[] expected = new long[] {3, 3, 5, 5, 6, 7};
Giải thích
Windows [1,3,-1], [3,-1,-3], [-1,-3,5], … have maxima 3, 3, 5, 5, 6, 7.
Đầu vào:
k = 1
numbers = [4, 2]
Kết quả mong đợi:
expected = [4, 2]
Đầu vào:
long long k = 1;
std::vector<long long> numbers = {4, 2};
Kết quả mong đợi:
std::vector<long long> expected = {4, 2};
Đầu vào:
long k = 1;
long[] numbers = new long[] {4, 2};
Kết quả mong đợi:
long[] expected = new long[] {4, 2};
Giải thích
Windows of one element are the elements themselves: [4, 2].
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.