baekjoon, programmers

programmers 캐시

leon-mcd 2024. 7. 25. 14:51

programmers

level2 캐시

문제 설명

프로그래머스 로고
위 이미지 클릭하면 해당 문제로 이동합니다.

#include <string>
#include <vector>
#include <algorithm>
#include <cctype>

using namespace std;

int find_cache(vector<string> &c, string city){
    int result;
    if(c.size()!=0){
        vector<string>::iterator i = find(c.begin(), c.end(), city);
        if(i==c.end()){
            c.erase(c.begin());
            c.push_back(city);
            result = 5;
        }
        else{
            c.erase(i);
            c.push_back(city);
            result = 1;
        }
    }
    else{
        result=5;
    }

    return result;
}

int solution(int cacheSize, vector<string> cities) {
    int answer = 0;
    vector<string> cache(cacheSize, " ");

    for(string i : cities){
        for(int j = 0; j < i.size(); j++) i[j]=toupper(i[j]);    //대문자로 변환
        answer+=find_cache(cache, i);
    }

    return answer;
}

해결 과정

간단한 구현 문제였다. deque를 사용해서 front_pop()을 사용하는 방법도 생각해봤지만 erase이용해서 vector로 해결했다. cache의 크기가 0인 경우를 고려하지 못해서 if문 추가하여 해결하였다.

'baekjoon, programmers' 카테고리의 다른 글

programmers 기능 개발  (0) 2024.07.25
programmers 할인 행사  (0) 2024.07.21
programmers H-Index  (0) 2024.07.21