Programming/Algorithm
[Python][프로그래머스] 보석 쇼핑
euphoria0-0
2020. 7. 11. 15:17
#2020카카오인턴십 문제
from collections import Counter
def solution(gems):
ans = [0,len(gems)]
n = len(set(gems))
i,j = 0,n # i,j커서를 움직임. j는 최소길이 n부터 시작
sets = Counter(gems[i:j]) # count sets
while (i >= 0 and j < len(gems)):
if len(sets) == n:
if j-i < ans[1]-ans[0]:
ans = [i,j]
else:
sets[gems[i]] -= 1
if sets[gems[i]] == 0:
del sets[gems[i]]
i += 1
else:
k = gems[j]
j += 1
if k not in sets:
sets[k] = 1
else:
sets[k] += 1
return [ans[0]+1, ans[1]]