보석 쇼핑
 
제출한 풀이
function solution(gems) {
    
    const gemsSet = new Set(gems)
    const collection = new Map()

    let left = -1
    let right = -1    
    
    const possible = []
    
    while(true){
        if(right === gems.length) break;
        
        if(collection.size < gemsSet.size){
            right++

            const gem = gems[right]
            
            collection.set(gem, collection.has(gem) ? collection.get(gem) + 1 :  1)
        }else{
            left++
            
            const gem = gems[left]
            const gemCount = collection.get(gem)
            
            if(gemCount === 1){
                possible.push([left +1, right +1])
                collection.delete(gem)
                continue
            }
            
            collection.set(gem, gemCount -1)
        }
    }
    
    
    possible.sort((a,b) => (a[1] - a[0]) - (b[1] - b[0]))
    
    const min = possible[0]
                         
    return min
}