Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- AI Hub
- mysqld.sock
- 마우스 따라다니기
- 걷는건귀찮아
- 3273
- 우분투
- 강의실2
- 원형
- 탄막
- 영상 프레임 추출
- 백준
- 3344
- 윈도우
- 2020 KAKAO BLIND RECRUITMENT
- 단어 수학
- 알고리즘
- 그리디알고리즘
- 회의실 배정
- MySQL
- SWEA
- 18249
- 알고리즘 목차
- 탄막 스킬 범위
- 자료구조 목차
- 토글 그룹
- 유니티
- 탄막 이동
- c#
- 수 만들기
- 문자열 압축
Archives
- Today
- Total
와이유스토리
[큐] 프로그래머스 [1차] 캐시 C++ 본문
#include <string>
#include <vector>
#include <queue>
using namespace std;
string toLower(string str) {
for (int i = 0; i < str.size(); i++) {
str[i] = tolower(str[i]);
}
return str;
}
int solution(int cacheSize, vector<string> cities) {
int answer = 0;
queue<string> q;
for (auto i : cities) {
bool isFind = false;
int range = cacheSize > q.size() ? q.size() : cacheSize;
for (int j = 0; j < range; j++) {
if (q.empty()) break;
if (q.front() == toLower(i)) {
answer++;
isFind = true;
}
else q.push(q.front());
q.pop();
}
if (!q.empty() && (q.size() == cacheSize)) q.pop();
if (!isFind) answer += 5;
q.push(toLower(i));
}
return answer;
}
* 참고
잘못된 풀이
// 실패
#include <string>
#include <vector>
#include <queue>
using namespace std;
string toLower(string str) {
for (int i = 0; i < str.size(); i++) {
str[i] = tolower(str[i]);
}
return str;
}
int solution(int cacheSize, vector<string> cities) {
int answer = 0;
queue<string> q;
for(auto i : cities) {
bool isFind = false;
for(int j=0; j<cacheSize; j++) {
if(q.empty()) break;
if(j == q.size()) break; // 중간에 큐 사이즈 변함
if(q.front() == toLower(i)) {
answer++;
isFind = true;
}
else q.push(q.front());
q.pop();
}
if(!q.empty() && (q.size() == cacheSize)) q.pop();
q.push(toLower(i));
if(!isFind) answer+=5;
}
return answer;
}
'코딩테스트 > 구현|기타' 카테고리의 다른 글
[연결리스트] (CHECK) 프로그래머스 표 편집 C++ (0) | 2023.02.11 |
---|---|
[소수] (CHECK) 프로그래머스 k진수에서 소수 개수 구하기 C++ (0) | 2023.01.21 |
[큐] 프로그래머스 두 큐 합 같게 만들기 C++ (0) | 2023.01.20 |
[해시] 프로그래머스 전화번호 Java (0) | 2022.12.16 |
[집합] 프로그래머스 튜플 Python (0) | 2022.02.04 |
Comments