Make Unreal REAL.
article thumbnail
Level 3. 네트워크

Level 3. 네트워크 i와 j 컴퓨터가 연결된 경우, i와 j의 연결을 끊고 i와 j부터 DFS를 수행해 연결된 컴퓨터들을 처리한다. #include #include using namespace std; void rDFS(int i, int j, int n, vector& computers) { computers[i][j] = computers[j][i] = 0; for (int k = 0; k < n; ++k) if (computers[i][k]) rDFS(i, k, n, computers); for (int k = 0; k < n; ++k) if (computers[j][k]) rDFS(j, k, n, computers); } int solution(int n, vector computers) {..

article thumbnail
Level 3. 단어 변환

Level 3. 단어 변환 전형적인 BFS 문제다. 최솟값을 구해야하므로 DFS가 아닌 BFS를 사용해야 한다. #include #include #include #include #include using namespace std; bool canConvert(string& a, string& b) { static const int DIFF = 1; int diff = 0; for (int i = 0; i < a.length(); ++i) diff += (a[i] != b[i]); return (diff == DIFF); } int solution(string begin, string target, vector words) { int answer = 0; queue que; unordered_map isU..

article thumbnail
Level 3. 베스트앨범

Level 3. 베스트앨범 장르별 총 플레이 시간을 계산하기 위한 맵 1개, 장르별 플레이 타임이 가장 긴 곡 2개를 추리기 위한 맵 1개를 사용해 해결했다. #include #include #include #include #include using namespace std; vector solution(vector genres, vector plays) { vector answer; size_t N = genres.size(); unordered_map u1; unordered_map u2; for (int i = 0; i < N; ++i) { u1[genres[i]] += plays[i]; u2[genres[i]].push_back({plays[i], i}); } vector v1(u1.begin()..

article thumbnail
Level 3. 이중우선순위큐

Level 3. 이중우선순위큐 출제자의 의도는 우선순위 큐 2개를 사용하라는 것 같은데 그냥 set으로 쉽게 풀었다. 위와 같은 상황에서는 set을 사용하는 게 훨씬 간단하고 효율적이기 때문이다. #include #include #include #include using namespace std; vector solution(vector operations) { multiset mset; for (string& op : operations) { int num = stoi(op.substr(2)); switch (op[0]) { case 'I': mset.emplace(num); break; case 'D': if (mset.empty()) continue; else if (0 < num) mset.era..

article thumbnail
Level 3. 정수 삼각형

Level 3. 정수 삼각형 위에서부터 아래로 내려오면서 좌측 부모, 우측 부모 중 큰 값을 더하는 방식이다. 그렇다보니 양쪽에서 인덱스 계산이 다소 번거롭다. #include #include #include using namespace std; int solution(vector triangle) { int answer = triangle[0][0]; for (int i = 1; i < triangle.size(); ++i) { for (unsigned j = 0; j < triangle[i].size(); ++j) { int l_parent = (1

article thumbnail
Level 2. 거리두기 확인하기

Level 2. 거리두기 확인하기 하나의 자리에 대해 마름모 모양으로 주변 12가지 자리에 대해 검사하면 되므로, 그냥 모든 경우를 구현했다. #include #include using namespace std; bool check_distancing(vector& place, int i, int j) { int row = place.size(); int col = place.front().length(); auto check_bound = [row, col](int i, int j) { return (0

검색 태그