Ctrl K

Shortest Span With the Top Frequency

Easy
You are given a non-empty array of non-negative integers values. Call its **peak frequency** the largest number of times any single value occurs in it. Return the length of the shortest contiguous segment of values whose peak frequency equals that of the whole array.
Example 1
values=[5, 1, 5, 3, 3, 2] → 2

The most frequent values, 5 and 3, each appear twice. Both 5s fit in a segment of length 3, and both 3s in the segment [3, 3] of length 2.

Example 2
values=[2, 6, 6, 2, 6, 1] → 4

6 appears three times, at positions 1, 2 and 4, so the segment [6, 6, 2, 6] of length 4 is needed.

Example 3
values=[8] → 1
Constraints
  • 1 ≤ len(values) ≤ 50,000
  • 0 ≤ values[i] ≤ 49,999
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasyThe Candidate With Most Votes EasyDoes Any Value Repeat EasyRepeat Within Reach EasyThe Lost Ticket Easy
loading Python…
Input
values=[5, 1, 5, 3, 3, 2]
Expected
2

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