[1차] 캐시

2020.08.01

const solution = (cacheSize, cities) => {
  const cache = [];
  let count = 0;
  if (cacheSize == 0) {
    return cities.length * 5;
  }
  for (let i = 0; i < cities.length; i++) {
    const current = cities[i].toLowerCase();
    const indexForSplice = cache.indexOf(current);
    if (indexForSplice != -1) {
      count++;
      cache.splice(indexForSplice, 1);
    } else {
      count += 5;
      if (cache.length == cacheSize) {
        cache.shift();
      }
    }
    cache.push(current);
  }
  return count;
};
  • 이거는 연습이니까 풀었지 내 실력으로는 실전에서 예외 절대 못 찾을 거 같다ㅠ

0개의 댓글