문제

https://school.programmers.co.kr/learn/courses/30/lessons/17680

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

내가 푼 풀이

 

#include <string>
#include <vector>

using namespace std;

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

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

        bool Is_hit = false;
        for (int j = 0; (j < cache.size() && j < cacheSize); j++)
        {
            if (cache[j] == cities[i])
            {
                cache.erase(cache.begin() + j);
                cache.push_back( cities[i]);
                Is_hit = true;
                break;
            }
        }

        if (Is_hit) answer++;
        else
        {
            answer += 5;        
            if (cache.size() == cacheSize)cache.erase(cache.begin());
            cache.push_back(cities[i]);
        }       
    }
    return answer;
}
Posted by pi92

블로그 이미지
pi92

공지사항

Yesterday
Today
Total

달력

 « |  » 2025.11
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

최근에 올라온 글

최근에 달린 댓글

글 보관함