슈콩

[프로그래머스] Lv.2 [1차] 캐시 본문

Algorithms/Programmers

[프로그래머스] Lv.2 [1차] 캐시

shukong 2025. 10. 15. 15:21

 

[문제]

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

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

[소스 코드]

import java.util.*;
class Solution {
    public int solution(int cacheSize, String[] cities) {
        int answer = 0;
        Queue<String> q = new LinkedList<>();
        for(int i=0;i<cities.length;i++){
            String c = cities[i].toLowerCase();
            if(q.contains(c)){
                q.remove(c);
                q.offer(c);
                answer++;
                continue;
            }
            else{
                if(cacheSize!=0){
                    if(cacheSize==q.size()){
                        q.poll();
                    }
                    q.offer(c);
                }
                answer += 5;
            }
        }
        return answer;
    }
}