https://www.welcomekakao.com/learn/courses/30/lessons/17680

 

코딩테스트 연습 - [1차] 캐시

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, S

www.welcomekakao.com

#include <iostream>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

int solution(int cacheSize, vector<string> cities) {
    int answer = 0;
    if (cacheSize == 0) {
        return cities.size() * 5;
    }

    for (int i = 0; i < cities.size() ; i++)
    {
        for (int j = 0; j < cities[i].size(); j++)
        {
            cities[i][j] = tolower(cities[i][j]);
        }
    }

    vector <string> rcities;
    for (int i = 0; i < cities.size(); i++)
    {
        int j = 0;
        bool flag = true;
        for ( j = 0; j < rcities.size(); j++)
        {
            if (rcities[j] == cities[i]) {
                string temp = rcities[j];
                rcities.erase(rcities.begin() + j);
                rcities.push_back(temp);
                flag = false;
                answer++;
                break;
            }
        }
        if (flag) {
            if (cacheSize == rcities.size()) {
                rcities.erase(rcities.begin());
                rcities.push_back(cities[i]);
            }
            else {
                rcities.push_back(cities[i]);
            }
            answer += 5;
        }
    }
    return answer;
}

+ Recent posts