title: "[프로그래머스] 보석 쇼핑 Python 파이썬 해설 (Level 3) - 이도훈"
cleanUrl: "programmers/67258"
description: "프로그래머스 Level 3 문제 [보석 쇼핑]의 풀이를 정리합니다."

문제 설명 및 제한사항

아이디어 및 해결 방법

코드

from collections import Counter

def solution(gems):
    # 투 포인터로 해결합니다.
    answers = []
    nunique = len(set(gems))
    
    cnt = Counter()
    i, j = 0, 0
    while j < len(gems):
        while j < len(gems) and len(cnt) != nunique:
            cnt[gems[j]] += 1
            j += 1
        
        while i < len(gems) and len(cnt) == nunique:
            cnt[gems[i]] -= 1
            if cnt[gems[i]] == 0:
                cnt.pop(gems[i])
                
            i += 1
            
        answers.append((j-i, i, j))
    
    answers.sort()
    return answers[0][1], answers[0][2]

출처

프로그래머스 코딩테스트 연습 https://school.programmers.co.kr/learn/challenges