Degree of an Array
Real-world version: The Shortest Stretch Holding the Commonest ReadingThe statement here is our own: the same inputs, outputs and technique.
A log holds the readings readings. Its busiest count is the number of times its most repeated reading appears.
Return the length of the shortest stretch of neighbouring entries whose own busiest count matches the whole log's.
Examples
Example 1
- Input
- readings = [4, 7, 7, 9, 4]
- Output
- 2
The busiest count is two, reached by both 4 and 7. The two 4s sit at the far ends and need the whole log, while the two 7s sit side by side, so the shorter stretch wins.
Example 2
- Input
- readings = [3, 3, 3, 1, 5]
- Output
- 3
Only 3 reaches the busiest count of three, and its appearances run from the start to the third entry.
Example 3
- Input
- readings = [8]
- Output
- 1
One entry, so the busiest count is one and a single entry already matches it.
Constraints
- 1 <= readings.length <= 50000
- 0 <= readings[i] <= 49999
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def find_shortest_sub_array(readings: list[int]) -> int:public int findShortestSubArray(int[] readings)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's