Ctrl K

Calm Before and After

Medium
A museum wants to pick a day to restore one of its halls. It has an attendance forecast: visitors[i] is how many guests are expected on day i (days are 0-based). You are also given a number span. Day i works if: - there are at least span days both before and after it; - over the span days leading up to day i, attendance never goes up: visitors[i - span] ≥ … ≥ visitors[i]; - over the span days after it, attendance never goes down: visitors[i] ≤ … ≤ visitors[i + span]. Return all such days in ascending order.
Example 1
visitors=[9, 7, 7, 4, 6, 8, 3], span=2 → [3]

Day 3: before it 7 ≥ 7 ≥ 4, after it 4 ≤ 6 ≤ 8. No other day works.

Example 2
visitors=[5, 5, 5], span=1 → [1]

The numbers never change, so the only day with a neighbour on each side qualifies.

Example 3
visitors=[3, 1, 2], span=0 → [0, 1, 2]

With span = 0 every day qualifies.

Constraints
  • 1 ≤ len(visitors) ≤ 10⁵
  • 0 ≤ visitors[i] ≤ 10⁵
  • 0 ≤ span ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSplit for the Best Averages MediumThe Growing Grab Limit MediumSegments With an Odd Sum MediumValley Floor Points MediumPass the Load to the Left Medium
loading Python…
Input
visitors=[9, 7, 7, 4, 6, 8, 3], span=2
Expected
[3]

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