![article thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FyT2kA%2FbtsbDEx6nKI%2F1eYYuSnijd6xU8k9ChsM91%2Fimg.png)
Level 2. [1차] 캐시 이 문제는 삽질을 좀 했다. 우선 컴퓨터 구조론 때 분명 구현해봤었는데, 너무 오래돼서 그런지 Hit 시에 앞으로 옮기는 걸 깜빡하고 있었다. 따라서, 원소의 우선순위를 조정할 수 없는 우선순위 큐를 쓰면 안 된다. unordered_set을 사용하기도 해봤지만, GCC에서는 삽입된 순서를 유지하지 않아 실패했다. 결국, 우선순위 큐나 해시 맵을 통해 시간 복잡도를 더 줄일 수는 없다는 걸 깨닫고 리스트로 해결했다. #include #include #include #include #include using namespace std; int solution(int cacheSize, vector cities) { int answer = 0; list lst; if (cache..
![article thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fbykxrb%2FbtsbDtpW3Lk%2FFkXBMj60kSKMJRKAAWKi31%2Fimg.png)
unordered_set의 입력 순서에 따른 원소 순서는 MSVC와 GCC에서 다르다. #include #include using namespace std; void main() { unordered_set uset; uset.emplace(1); uset.emplace(2); uset.emplace(3); uset.emplace(4); uset.emplace(5); uset.emplace(6); for (int n : uset) cout
![article thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbyW63d%2Fbtsbn6BCEzF%2FEbzd4wbJ2bEULik8js4zSk%2Fimg.png)
Level 2. 예상 대진표 대결하는 두 참자가 중 앞 번호의 선수는 항상 홀수이고, 뒷 번호 선수와의 번호 차이는 1이다. 대진표가 이진 트리의 형식이기 때문에, 부모 노드의 번호는 2를 나누면 된다. #include #include using namespace std; int solution(int n, int a, int b) { int answer = 1; if (a > b) swap(a, b); while (~a & 1 || a + 1 != b) { ++a /= 2; ++b /= 2; ++answer; } return answer; } 불필요한 계산을 줄이고 비트 연산을 사용하면 다음과 같이 간소화시킬 수도 있다. #include using namespace std; int solution(in..
![article thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FpJvlH%2FbtsaUjVBxHK%2FGmzmjwGGoChSckE0PHkyXK%2Fimg.png)
Level 1. 시저 암호 반복문을 돌 필요 없이 나머지 연산을 이용하면 된다. #include #include using namespace std; string solution(string s, int n) { transform(s.begin(), s.end(), s.begin(), [n](char c) -> char { if (!isalpha(c)) return c; return isupper(c) ? 'A' + (c + n - 'A') % 26 : 'a' + (c + n - 'a') % 26; }); return s; }
![article thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FxlETS%2FbtsatVaj09e%2FOMvDxQMvmOJUa1ckTSUUp0%2Fimg.png)
Level 1. 공원 산책 시작점을 직접 찾아야 하고 좌표가 2차원 벡터가 아닌, 문자열 벡터로 주어져서 조금 까다로운 문제였다. #include #include using namespace std; vector solution(vector park, vector routes) { vector current(2); int H = park.size(), W = park.front().length(); auto dir = [](string& s) -> vector { switch (s[0]) { case 'E': return vector {0, 1}; case 'W': return vector {0, -1}; case 'S': return vector {1, 0}; case 'N': return vector..
![article thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbbezfK%2Fbtsakd3vwDk%2FBnKWTkvoDr5YYJD2kHHW1k%2Fimg.png)
Level 1. 달리기 경주 매번 인덱스를 찾을 필요 없이, 해시 맵에 인덱스를 저장하고 정렬 상태를 유지할 필요가 없으므로 unordered_map을 사용한다. 플레이어 이름을 바꿀 때, 인덱스도 같이 바꿔줘야 함에 주의한다. #include #include #include using namespace std; vector solution(vector players, vector callings) { unordered_map umap; for (int i = 0; i < players.size(); ++i) umap[players[i]] = i; for (string calling : callings) { string& p1 = players[umap[calling] - 1]; string& p2 = ..