짱아의 개발 기록장

2018 KAKAO BLIND RECRUITMENT : 캐시(c++) 본문

Algorithm/카카오 기출

2018 KAKAO BLIND RECRUITMENT : 캐시(c++)

jungahshin 2021. 3. 9. 22:41
반응형

설명 추가 예정

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <string>
#include <vector>
#include <climits>
 
using namespace std;
 
string toLower(string s){
    string temp = "";
    for(int i=0; i<s.size(); i++){
        temp += tolower(s[i]);
    }
    
    return temp;
}
 
int solution(int cacheSize, vector<string> cities) {
    int answer = 0;
    
    if(cacheSize==0){
        return (5*cities.size());
    }
    
    int idx = 0;
    int num = 0// 캐시의 크기
    vector<string> cache;
    
    while(idx<cities.size()){
        
        // cass hit
        string temp = "";
        bool check = false;
        for(int i=0; i<cache.size(); i++){
            if(toLower(cache[i])==toLower(cities[idx])){
                temp = cache[i];
                cache.erase(cache.begin()+i);
                cache.push_back(temp);
                idx++;
                check = true;
                break;
            }
        }
        if(check==true){
            answer += 1;
            continue;
        }
        
        // cass miss
        if(num<cacheSize){
            cache.push_back(cities[idx]);
            idx++;
        }else{
            cache.erase(cache.begin()+0);
            cache.push_back(cities[idx]);
            idx++;
        }
        answer += 5;
        num++;
    }
    
    return answer;
}
cs
반응형
Comments