Level
프로그래머스 Lv2
Day
13일
kakao
2018 카카오 블라인드
 
function solution(cacheSize, cities) {
    const HIT = 1;
    const MISS = 5;
    
    if(cacheSize===0){
        return cities.length * MISS;
    }
    
    const cache = new Set();
    let time = 0;
    
    cities.forEach(c => {
        const city = c.toLowerCase();
        
        if(cache.has(city)){
            time += HIT;
            cache.delete(city);
            cache.add(city);
        }else{
            if(cache.size===cacheSize){
                const LRUvalue = cache[Symbol.iterator]().next().value
                cache.delete(LRUvalue)
            }
            cache.add(city);
            time += MISS;
        }
    })
    
    return time;
}