
Level 2. 괄호 회전하기 rotate() 함수로 하나씩 밀면서 확인하면 된다. #include #include #include using namespace std; int solution(string s) { int answer = 0; size_t len = s.length(); auto check = [](string& s) -> bool { stack stck; for (char c : s) { if (stck.empty() || c == '(' || c == '{' || c == '[') stck.emplace(c); else if ((stck.top() == '(' && c == ')') || (stck.top() == '{' && c == '}') || (stck.top() == '[' ..

Level 2. 가장 큰 수 단순히 정렬만 해서는 안 된다. 단순 문자열 정렬 시, [3, 30] -> 303이 되기 때문이다. 비교할 두 문자열을 직접 붙여 비교해봐야 한다. 모든 수가 0인 경우를 주의해야 한다. #include #include #include #include #include using namespace std; string solution(vector numbers) { sort(numbers.begin(), numbers.end(), [](int a, int b) { string A = to_string(a), B = to_string(b); return A + B > B + A; }); return accumulate(numbers.begin(), numbers.end(), st..

Level 2. 카펫 노란색 격자의 가로 개수를 W, 세로 개수를 H로 한다. 2W + 2H + 4 = Brown 2(Yellow / H) + 2H + 4 = Brown 2(Yellow / H) + 2H + 4 - Brown = 0 2H^2 + 2Yellow + (4 - Brown)H = 0 변수 H에 대한 이차 방정식을 풀면 된다. 개수는 자연수이기 때문에 단순하게 1부터 증가시켜 해결했지만, 물론 근의 공식으로 풀 수도 있다. #include #include using namespace std; vector solution(int brown, int yellow) { float h = 0; while (2 * ++h + 2 * yellow / h + 4 != brown); return vector {..

Level 2. 2개 이하로 다른 비트 num부터 1씩 증가시키면서 비트셋과 XOR 연산을 이용해 다른 비트의 개수를 찾는 풀이이다. 하지만, 이 풀이는 시간 초과가 발생한다. // 이 풀이는 시간 초과가 발생한다. #include #include #include #include using namespace std; vector solution(vector numbers) { auto func = [](long long num) { int diff = 0; bitset bset(num); while (diff != 1 && diff != 2) diff = (bset ^ bitset(++num)).count(); return num; }; transform(numbers.begin(), numbers.en..

Level 2. 전화번호 목록 길이 순으로 정렬한 후 앞에서부터 길이가 짧은 순으로 비교했다. 길이가 긴 문자열이 짧은 문자열의 접두어가 될 수는 없기 때문이다. 하지만, 이 풀이는 시간 초과가 발생한다. // 이 풀이는 시간 초과가 발생한다. #include #include #include using namespace std; bool solution(vector phone_book) { size_t size = phone_book.size(); sort(phone_book.begin(), phone_book.end(), [](string a, string b) { return a.length() < b.length(); }); for (int i = 0; i < size - 1; ++i) for (i..

Level 2. 위장 경우의 수로 생각하면, 각 종류별로 하나만 착용한 경우와 하나도 착용하지 않은 경우가 있다. 하나만 착용한 경우는 각 종류별 옷의 개수이고, 하나도 착용하지 않은 경우는 1이다. 각 종류별 옷의 개수 + 1을 다 곱해주고, 마지막으로 아무 것도 입지 않는 경우의 수 1을 빼주면 된다. #include #include #include using namespace std; int solution(vector clothes) { int answer = 1; unordered_map umap; for (vector clothe : clothes) ++umap[clothe[1]]; for (auto& pr : umap) answer *= pr.second + 1; return answer -..