TypeScriptHardStack & Queue40 min

Find every sliding window maximum

For a nonempty array and 1 <= k <= its length, return the maximum of each consecutive window of length k. Inputs are valid JSON and must not be modified unless the algorithm uses a local copy. This exercise probes typed data modeling, boundary handling, and algorithm selection.

Input

One JSON value: { values: number[]; k: number }. See the sample for its exact shape.

Output

One compact JSON value of type number[]. Strings include JSON quotes; arrays preserve the specified order.

Constraints

Array/string lengths are 0 to 10000 unless stated otherwise. Numbers are integers in [-1000000,1000000]; all computed results fit the safe integer range.

Example

Input

{"values":[1,3,-1,-3,5,3,6,7],"k":3}

Output

[3,3,5,5,6,7]

Example 2 input

{"values":[3,2,1],"k":1}

Output

[3,2,1]

Checks a boundary or contrasting case beyond the public examples.

Public test cases (2)

  • Input

    {"values":[1,3,-1,-3,5,3,6,7],"k":3}

    Expected output

    [3,3,5,5,6,7]
  • Input

    {"values":[1],"k":1}

    Expected output

    [1]
Hints (2)
  1. Start with the required output order and the empty-input behavior.
  2. Keep candidate indices in a decreasing monotonic deque

Target complexity: time O(n) · space O(n)

Your solution

Checking runner availability…

Reference solution

Sign in for solution
All TypeScript programs