1. 2018 KAKAO BLIND RECRUITMENT [1차] 캐시

이동한·2023년 6월 28일
0

알고리즘 기출

목록 보기
18/22
import java.util.LinkedList;

class Solution {
    public int solution(int cacheSize, String[] cities) {
        int len = cities.length;
        if(cacheSize == 0) return len*5;
        
        int answer = 0;
        LinkedList<String> q = new LinkedList<>();
        
        for(int i=0; i<len; i++){
            String cur = cities[i].toLowerCase();
            if(q.remove(cur)){
                // 사용되었으므로 재갱신
                q.add(cur); 
                answer+=1;
            }else{
                answer+=5;
                if(q.size()>=cacheSize){
                    // 가장 오래된 녀석 제거
                    q.remove(0); 
                }
                q.add(cur);
            }
        }
        
        return answer;
    }
}

LinkedList.remove(int idx) 인덱스의 원소 삭제 후 해당되는 원소반환
LinkedList.remove(String value) -> value가 있으면 삭제하고 true반환 없으면 false 반환

profile
Pragmatic, Productive, Positivist

0개의 댓글