Make Unreal REAL.
article thumbnail
Level 2. [1차] 캐시

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
Level 2. 예상 대진표

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
Level 1. 시저 암호

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
Level 1. 공원 산책

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
Level 1. 달리기 경주

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 = ..

article thumbnail
Level 2. [3차] 파일명 정렬

Level 2. [3차] 파일명 정렬 비교에는 HEAD와 NUMBER만 사용하고, TAIL의 조건인 순서 유지는 stable_sort() 함수로 정렬하면 된다. parse 람다 함수에서 알파벳을 소문자로 만든 HEAD와 NUMBER를 파싱하고 조건대로 정렬해 해결했다. #include #include #include #include using namespace std; vector solution(vector files) { auto parse = [](const string& s) -> pair { pair pr; auto it = find_if(s.begin(), s.end(), ::isdigit); pr.first = string(s.begin(), it); transform(pr.first.beg..

검색 태그