Algorithm
[프로그래머스 17682 - 카카오 2018 1차] 캐시
승우승
2020. 5. 2. 11:20
반응형
문제 출처
https://programmers.co.kr/learn/courses/30/lessons/17680
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제풀이
도시이름을 소문자로 모두 바꾼뒤 풀이해야한다. cache 벡터를 사용해서 사용해서 매개변수로 주어진 cacheSize보다 크거나 같을 때 가장 앞에 있는 원소를 삭제해주면 된다. 문제에서 시키는데로 그냥 구현하면 되는 문제.
소스코드
https://github.com/sw93/algorithm/blob/master/Programmers/kakao2018%20-%20%EC%BA%90%EC%8B%9C.cpp
sw93/algorithm
problem solve. Contribute to sw93/algorithm development by creating an account on GitHub.
github.com
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int solution(int cacheSize, vector<string> cities) {
int answer = 0;
vector<string> cache;
if (cacheSize == 0) {
return cities.size() * 5;
}
for (int i=0; i<cities.size(); i++) {
transform(cities[i].begin(), cities[i].end(), cities[i].begin(), ::tolower);
vector<string>::iterator it = find(cache.begin(), cache.end(), cities[i]);
if (it == cache.end()) {
if (cache.size() >= cacheSize) cache.erase(cache.begin());
cache.push_back(cities[i]);
answer+=5;
} else {
cache.erase(it);
cache.push_back(cities[i]);
answer++;
}
}
return answer;
}
반응형