Make Unreal REAL.
article thumbnail
Level 2. 큰 수 만들기

Level 2. 큰 수 만들기 이미 k개만큼 제거했는데 뒤 숫자가 남았거나, answer가 비어있으면 삽입한다. answer의 마지막 수보다 작은 수면 삽입하고, 큰 수면 그 수보다 크거나 같은 수가 나올 때까지 1개씩 제거하면서 answer에서 다시 뺀 후 삽입한다. 솔직히 미리 알고리즘을 생각하고 풀었다기보다는 그냥 조건에 맞춰 그때그때 추가하면서 풀었다. #include using namespace std; string solution(string number, int k) { size_t len = number.length(); string answer; for (int i = 0; i =..

article thumbnail
Level 2. 다음 큰 숫자

Level 2. 다음 큰 숫자 앞에 비트를 추가하며 이진수로 변환하기 때문에 deque를 사용했다. 비트 배열에 next_permutation() 함수를 적용하면 정확히 이 문제의 정답을 찾을 수 있다. #include #include #include using namespace std; int solution(int n) { int answer = 0; deque binary; do binary.emplace_front(n & 1); while (n >>= 1); binary.emplace_front(0); next_permutation(binary.begin(), binary.end()); for (int bit : binary) (answer

article thumbnail
Level 2. 숫자의 표현

Level 2. 숫자의 표현 첫 번째 시도에서는 정답이었으나 시간 초과가 발생했다. DP를 통해 간단하게 각 수까지의 합을 구했으나, 모든 경우의 수를 확인해서 그런 것 같다. // 이 풀이는 시간 초과가 발생한다. #include #include using namespace std; int solution(int n) { int answer = 0; vector sum(n + 1, 0); for (int i = 1; i

article thumbnail
Level 2. 프린터

Level 2. 프린터 앞뒤에서 삽입, 삭제가 빈번하므로 벡터보다는 덱을 사용했다. 덱 하나에 (값, location)을 pair로 저장해도 되지만 편의상 따로 만들었다. priorities에 있는 중요도를 모두 정렬해놓고 시작하면, priorities의 마지막 원소를 통해 자신이 가장 높은 중요도를 갖는지, 자신보다 높은 중요도가 있는지 바로 확인할 수 있다. #include #include #include #include #include using namespace std; int solution(vector priorities, int location) { int answer = 0; int loc_printed = -1; deque deq(priorities.begin(), priorities.e..

article thumbnail
Level 2. 올바른 괄호

Level 2. 올바른 괄호 스택을 활용해 괄호 짝을 맞추었다. 스택이 비었거나 여는 괄호이면 스택에 넣는다. 스택에 뭐가 있는데 닫는 괄호이면 1개를 뺀다. 스택의 top은 항상 여는 괄호를 유지한다. push, pop 연산 후 top이 닫는 괄호면 즉시 false를 반환한다. #include #include using namespace std; bool solution(string s) { stack stck; for (char c : s) { if (stck.empty() || c == '(') stck.emplace(c); else stck.pop(); if (!stck.empty()) if (stck.top() == ')') return false; } return stck.empty(); } ..

article thumbnail
Level 2. 행렬의 곱셈

Level 2. 행렬의 곱셈 #include #include using namespace std; vector solution(vector arr1, vector arr2) { size_t M = arr1.size(), K = arr2.size(), N = arr2.front().size(); vector answer(M, vector(N, 0)); for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) for (int i = 0; i < K; ++i) answer[row][col] += arr1[row][i] * arr2[i][col]; return answer; }

검색 태그