알고리즘/프로그래머스

[2단계] KAKAO BLINE RECRUITMENT - 캐시

hongeeii 2023. 12. 4. 12:50
728x90
반응형

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

image

import java.util.LinkedList; import java.util.Queue; import java.util.Collection;  class Solution {     private static final int CACHE_MISS = 5;     private static final int CACHE_HIT = 1;      public static int solution(int cacheSize, String[] cities) {         int answer = 0;         Queue<String> cache = new LinkedList<>();          if (cacheSize == 0) {             return cities.length * CACHE_MISS;         }          for (String city : cities) {             if (containIgnoreUpperAndLowerCase(cache, city)) {                 // cache hit                 answer += CACHE_HIT;             } else {                 // cache miss                 answer += CACHE_MISS;                  if (cache.size() == cacheSize) {                     cache.poll();                 }             }              cache.add(city);         }          return answer;     }           private static boolean containIgnoreUpperAndLowerCase(Collection<String> collection,             String city) {         for (String object : collection) {             if (object.equalsIgnoreCase(city)) {                 collection.remove(object);                 return true;             }         }          return false;     } } 
728x90
반응형