[2018 KAKAO BLIND RECRUITMENT 1차] 캐시


문제설명

캐시

지도개발팀에서 근무하는 제이지는 지도에서 도시 이름을 검색하면 해당 도시와 관련된 맛집 게시물들을 데이터베이스에서 읽어 보여주는 서비스를 개발하고 있다. 이 프로그램의 테스팅 업무를 담당하고 있는 어피치는 서비스를 오픈하기 전 각 로직에 대한 성능 측정을 수행하였는데, 제이지가 작성한 부분 중 데이터베이스에서 게시물을 가져오는 부분의 실행시간이 너무 오래 걸린다는 것을 알게 되었다. 어피치는 제이지에게 해당 로직을 개선하라고 닦달하기 시작하였고, 제이지는 DB 캐시를 적용하여 성능 개선을 시도하고 있지만 캐시 크기를 얼마로 해야 효율적인지 몰라 난감한 상황이다.

어피치에게 시달리는 제이지를 도와, DB 캐시를 적용할 때 캐시 크기에 따른 실행시간 측정 프로그램을 작성하시오.

입력 형식

  • 캐시 크기(cacheSize)와 도시이름 배열(cities)을 입력받는다.
  • cacheSize는 정수이며, 범위는 0 ≦ cacheSize ≦ 30 이다.
  • cities는 도시 이름으로 이뤄진 문자열 배열로, 최대 도시 수는 100,000개이다.
  • 각 도시 이름은 공백, 숫자, 특수문자 등이 없는 영문자로 구성되며, 대소문자 구분을 하지 않는다. 도시 이름은 최대 20자로 이루어져 있다.

출력 형식

  • 입력된 도시이름 배열을 순서대로 처리할 때, 총 실행시간을 출력한다.

조건

  • 캐시 교체 알고리즘은 LRU(Least Recently Used)를 사용한다.
  • cache hit일 경우 실행시간은 1이다.
  • cache miss일 경우 실행시간은 5이다.

입출력 예제

캐시크기(cacheSize)도시이름(cities)실행시간
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, Rome, Paris, Jeju, NewYork, Rome]60
5[Jeju, Pangyo, Seoul, NewYork, LA, SanFrancisco, Seoul, Rome, Paris, Jeju, NewYork, Rome]52
2[Jeju, Pangyo, NewYork, newyork]16
0[Jeju, Pangyo, Seoul, NewYork, LA]25

내가 생각하는 문제 풀이 포인트

  1. 캐시 구현 문제
  2. LRU에 대한 이해가 필요하다.
    1. LRU알고리즘은 가장 사용하지 않은 것을 내보내는 것이다.

코드

public class Solution {

    static int minTime = Integer.MAX_VALUE;
    static Cache[] memory;

    public static int solution(int cacheSize, String[] cities) {
        int answer = 0;
        if(cacheSize==0)
            return 5*cities.length;
        memory = new Cache[cacheSize];
        for (int i = 0; i < cacheSize; i++)
            memory[i] = new Cache(0, "");
        int time = 1;
        for (String city : cities) {
            answer += LRU(city, time);
            time++;
        }

        return answer;
    }

    private static int LRU(String city, int time) {
        int ptr = 0;
        city = city.toLowerCase();
        for (int i = 0; i < memory.length; i++) {
            if (memory[i].getCity().equals(city)) {//hit
                memory[i].setTime(time);
                return 1;
            }
        }

        //hit 안됐을 때
        for (int i = 0; i < memory.length; i++) {
            if (memory[i].getTime() < minTime) {
                ptr = i;
                minTime = memory[i].getTime();
            }
        }
        memory[ptr].setTime(time);
        minTime = time;
        memory[ptr].setCity(city);

        return 5;
    }

    static class Cache {
        private int time;

        public int getTime() {
            return time;
        }

        public void setTime(int time) {
            this.time = time;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        private String city;

        public Cache(int time, String city) {
            this.time = time;
            this.city = city;
        }
    }

    public static void main(String[] args) {
        int cacheSize = 3;
        String[] cities = {"Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul"};

        System.out.println(solution(cacheSize,cities));
    }
}

느낀점

컴퓨터 구조 시간에 구현했던 캐시에 관한 문제이다.

문제 링크

on Programmers, Java, Algorithm