https://programmers.co.kr/learn/courses/30/lessons/17680
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(int cacheSize, vector<string> cities) {
int answer = 0;
vector<string> qu;
for(int i=0;i<cities.size();i++){
string str = cities[i];
transform(str.begin(), str.end(), str.begin(), ::tolower);
bool check = false;
int idx;
for(int j=0;j<qu.size();j++){
if(str == qu[j]){
check = true;
idx = j;
}
}
if(check == true){ // 있으면
answer++;
qu.erase(qu.begin()+idx);
qu.push_back(str);
}else{ // 없으면
answer+=5;
if(cacheSize != 0){
if(qu.size() == cacheSize){
qu.erase(qu.begin());
}
qu.push_back(str);
}
}
}
return answer;
}
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(int cacheSize, vector<string> cities) {
vector <string> q;
int duration = 0;
for(vector <string>::iterator it = cities.begin(); it != cities.end(); it++){
transform(it->begin(), it->end(), it->begin(), ::tolower);
bool flag = false;
for(vector<string>::iterator itt = q.begin(); itt != q.end(); itt++){
if(*itt == *it) {
flag = true;
duration +=1;
q.erase(itt);
q.push_back(*it);
break;
}
}
if(!flag) {
duration +=5;
if(cacheSize != 0 && q.size() >= cacheSize)
q.erase(q.begin());
if(q.size() < cacheSize)
q.push_back(*it);
}
}
return duration;
}