Show problems as
Classic problem #697EasyArrayHash Table

Degree of an Array

Real-world version: The Shortest Stretch Holding the Commonest Reading
Tracked in this browser only
Read Degree of an Array on LeetCode(opens in a new tab)

The 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.

Python
def find_shortest_sub_array(readings: list[int]) -> int:
Java
public int findShortestSubArray(int[] readings)