Advertisement
rosien

shortestsubarray

Oct 5th, 2020 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. class Solution:
  2.     def findShortestSubArray(self, nums: List[int]) -> int:
  3.         freq = {}
  4.         value_to_index = {}
  5.         for ind, element in enumerate(nums):
  6.             freq[element] = freq.get(element,0) + 1
  7.             if element not in value_to_index:
  8.                 value_to_index[element]= [ind]
  9.             else:
  10.                 value_to_index[element].append(ind)
  11.         max_freq = max(freq.values())
  12.         distance = float('inf')
  13.         for key in freq:
  14.             if freq[key] == max_freq:
  15.                 cur_distance = max(value_to_index[key])- min(value_to_index[key]) + 1
  16.         distance = min(distance, cur_distance)
  17.  
  18.         return distance
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement