Created
July 11, 2017 08:48
-
-
Save jwz-ecust/f4d1d236d27777f0fd6167ae13ab3996 to your computer and use it in GitHub Desktop.
计算相距最远的两个相等元素
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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