Ctrl K

Staircase Windows

Medium
You are given an array values and a window length size. Call a run of consecutive elements a **staircase** if every element is exactly 1 greater than the one before it, like [7, 8, 9]. For each window of size consecutive elements, from left to right, record its last (largest) element if the window is a staircase, and -1 otherwise. Return the resulting array of length len(values) - size + 1.
Example 1
values=[4, 5, 6, 7, 5, 6], size=3 → [6, 7, -1, -1]

Windows: [4, 5, 6] → 6, [5, 6, 7] → 7, while [6, 7, 5] and [7, 5, 6] are not staircases → -1.

Example 2
values=[3, 3, 4], size=2 → [-1, 4]

[3, 3] steps by 0 instead of +1, so -1; [3, 4] is a staircase topped by 4.

Example 3
values=[9, 8, 7], size=1 → [9, 8, 7]

A one-number window is always a staircase, so the answer is the numbers themselves.

Constraints
  • 1 ≤ len(values) ≤ 500
  • 1 ≤ values[i] ≤ 10⁵
  • 1 ≤ size ≤ len(values)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Stretch to the Goal MediumEvenly Stepped Stretches MediumLongest Run of Ones With One Fix MediumNearest k Values MediumWindows With a Small Product Medium
loading Python…
Input
values=[4, 5, 6, 7, 5, 6], size=3
Expected
[6, 7, -1, -1]

Run checks the first 3 tests in your browser. Submit checks all 130 tests on the server, hidden ones included.