Skip to content

Instantly share code, notes, and snippets.

@jwz-ecust
Created July 11, 2017 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwz-ecust/f4d1d236d27777f0fd6167ae13ab3996 to your computer and use it in GitHub Desktop.
Save jwz-ecust/f4d1d236d27777f0fd6167ae13ab3996 to your computer and use it in GitHub Desktop.
计算相距最远的两个相等元素
from collections import Counter
from collections import defaultdict
def solution(A):
c = Counter(A)
repeat = [i for i in c if c[i] > 1]
ddict = defaultdict(list)
for i in range(len(A)):
ddict[A[i]].append(i)
dis = [max(ddict[i]) - min(ddict[i]) for i in ddict if len(ddict[i]) > 1]
return max(dis)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment