Problem statement

https://binarysearch.com/problems/Shortest-Sublist-With-Max-Frequency/

Solution

Equal to Leetcode 0697 Degree of an Array.

Complexity

Time and space complexity is O(n).

Code

class Solution:
    def solve(self, nums):
        last = {v: i for i, v in enumerate(nums)}
        first = {v: len(nums)-i-1 for i, v in enumerate(nums[::-1])}
        c = Counter(nums)
        degree = max(c.values())
        return min(last[v] - first[v] + 1 for v in c if c[v] == degree)