Notice
Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 1์ฐจ ์บ์‹œ (javaScript) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 1์ฐจ ์บ์‹œ (javaScript)

Lennon 2021. 10. 7. 17:33
728x90
๋ฐ˜์‘ํ˜•

https://programmers.co.kr/learn/courses/30/lessons/17680

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - [1์ฐจ] ์บ์‹œ

3 ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "Jeju", "Pangyo", "Seoul", "NewYork", "LA"] 50 3 ["Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul"] 21 2 ["Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Ro

programmers.co.kr

 

function solution(cacheSize, cities) {
    const city = cities.map(a => a.toLowerCase()); 
    // ๋ฐฐ์—ด ๋‚ด ์š”์†Œ ์†Œ๋ฌธ์ž๋กœ
    
    let queue = [];
    let time = 0;
    
    for(let i = 0; i < city.length; i++){
        if(!queue.includes(city[i])){ // ํ์— ๋„์‹œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด
            time+=5;
            queue.push(city[i]);
            if(queue.length > cacheSize){ // ์บ์‹œ์‚ฌ์ด์ฆˆ๋ฅผ ๋„˜์œผ๋ฉด ์•ž ์š”์†Œ ์‚ญ์ œ
                queue.shift();
            }
        }
        else{ // ๋„์‹œ๊ฐ€ ํ์— ์žˆ์œผ๋ฉด
            time++;
            let idx = queue.indexOf(city[i]); 
            queue.splice(idx, 1);
            queue.push(city[i]); // ์ธ๋ฑ์Šค ์ฐพ์•„์„œ ์ œ๊ฑฐ ํ›„ ๋งจ ๋’ค์— ์‚ฝ์ž…
        }
    }
    return time;
}
728x90
๋ฐ˜์‘ํ˜•
Comments