-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkakao-7.cpp
More file actions
66 lines (61 loc) · 1.22 KB
/
kakao-7.cpp
File metadata and controls
66 lines (61 loc) · 1.22 KB
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
62
63
64
65
66
// 캐시
// 2019.08.29
#include<string>
#include<vector>
#include<map>
#include<list>
using namespace std;
int solution(int cacheSize, vector<string> cities) {
int answer = 0;
map<string, bool> cache;
list<string> list;
for (int i = 0; i < cities.size(); i++)
{
for (int j = 0; j < cities[i].size(); j++)
{
cities[i][j] = tolower(cities[i][j]);
}
}
if (cacheSize == 0)
{
answer = cities.size() * 5;
}
else
{
for (int i = 0; i < cities.size(); i++)
{
// 현재 도시가 캐시에 들어있지 않다면(cache miss)
if (cache.find(cities[i]) == cache.end())
{
answer += 5;
cache[cities[i]] = true;
// 캐시가 꽉 차있다면 맨 앞꺼 제거(LRU)
if (list.size() == cacheSize)
{
cache.erase(list.front());
list.pop_front();
list.push_back(cities[i]);
}
else
{
list.push_back(cities[i]);
}
}
// 현재 도시가 캐시에 들어있다면(cache hit)
else
{
answer += 1;
// 들어있는거 찾아서 맨 뒤로 넣어줌(LRU)
for (auto iter = list.begin(); iter != list.end(); iter++)
{
if (*iter == cities[i])
{
list.erase(iter);
list.push_back(cities[i]);
}
}
}
}
}
return answer;
}