Make Unreal REAL.
article thumbnail
unordered_map은 역순회가 불가능하다.

How to get the last element of an std::unordered_map? How to get the last element of an std::unordered_map? myMap.rbegin() and --myMap.end() are not possible. stackoverflow.com unordered_map은 역순회가 불가능하므로, 다음과 같이 사용할 수 없다. 특이하게도 MSVC에서는 되는데, GCC에서는 작동하지 않는다. void func() { unordered_map umap {{"A", 1}, {"B", 2}, {"C", 3}}; // 런타임 오류 발생 ! ! auto it = prev(umap.end()); it = umap.end() - 1; } 그러므로 해시..

article thumbnail
Level 2. 추억 점수

Level 2. 추억 점수 그냥 이중 for문을 쓸 걸 그랬다. #include #include #include #include #include using namespace std; vector solution(vector name, vector yearning, vector photo) { vector answer(photo.size()); size_t size = name.size(); unordered_map umap; for (int i = 0; i < size; ++i) umap[name[i]] = yearning[i]; auto func = [&umap](int sum, string& s){ return sum + umap[s]; }; auto func2 = [&func](vector& v) ..

article thumbnail
Level 2. 최솟값 만들기

Level 2. 최솟값 만들기 A 배열의 작은 수부터, B 배열의 큰 수부터 차례로 곱해 더하면 된다. #include #include #include #include using namespace std; int solution(vector A, vector B) { sort(A.begin(), A.end()); sort(B.rbegin(), B.rend()); transform(A.begin(), A.end(), B.begin(), A.begin(), ::multiplies()); return accumulate(A.begin(), A.end(), 0); } 없을 줄 알았는데 내적을 계산해주는 inner_product() 함수가 STL에 있었다. #include #include #include #inc..

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
내적 계산

transform() 함수와 accumulate() 함수를 사용해 직접 구현하면 다음과 같다. #include #include #include #include using namespace std; void main() { vector A = {1, 2, 3}; vector B = {4, 5, 6}; transform(A.begin(), A.end(), B.begin(), A.begin(), ::multiplies()); cout

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

검색 태그