Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def findShortestSubArray(self, nums: List[int]) -> int:
- freq = {}
- value_to_index = {}
- for ind, element in enumerate(nums):
- freq[element] = freq.get(element,0) + 1
- if element not in value_to_index:
- value_to_index[element]= [ind]
- else:
- value_to_index[element].append(ind)
- max_freq = max(freq.values())
- distance = float('inf')
- for key in freq:
- if freq[key] == max_freq:
- cur_distance = max(value_to_index[key])- min(value_to_index[key]) + 1
- distance = min(distance, cur_distance)
- return distance
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement