Tutte le sfide
Difficile 0 risolte 0% di accettazione

Sliding Window Maximum

Report the maximum of every window of k numbers.

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.

Vincoli

1 ≤ k ≤ length ≤ 200,000

Esempi

Esempio 1
Input:
k = 3
numbers = [1, 3, -1, -3, 5, 3, 6, 7]

Output atteso:
expected = [3, 3, 5, 5, 6, 7]
Input:
long long k = 3;
std::vector<long long> numbers = {1, 3, -1, -3, 5, 3, 6, 7};

Output atteso:
std::vector<long long> expected = {3, 3, 5, 5, 6, 7};
Input:
long k = 3;
long[] numbers = new long[] {1, 3, -1, -3, 5, 3, 6, 7};

Output atteso:
long[] expected = new long[] {3, 3, 5, 5, 6, 7};

Spiegazione

Windows [1,3,-1], [3,-1,-3], [-1,-3,5], … have maxima 3, 3, 5, 5, 6, 7.

Esempio 2
Input:
k = 1
numbers = [4, 2]

Output atteso:
expected = [4, 2]
Input:
long long k = 1;
std::vector<long long> numbers = {4, 2};

Output atteso:
std::vector<long long> expected = {4, 2};
Input:
long k = 1;
long[] numbers = new long[] {4, 2};

Output atteso:
long[] expected = new long[] {4, 2};

Spiegazione

Windows of one element are the elements themselves: [4, 2].

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.