Make Unreal REAL.
article thumbnail
Level 0. 그림 확대

Level 0. 그림 확대 단순하게 for문으로 문자열을 계속 더하거나, vector::insert() 함수로 계속 삽입할 수도 있다. 나는 메모리 재할당을 최소화하기 위해 미리 공간을 확보해놓고 인덱스를 계산해 해결했다. #include #include #include using namespace std; vector solution(vector picture, int k) { for (string& pic : picture) { pic.resize(pic.length() * k); for (int i = pic.length(); 0 < i; i -= k) fill(pic.begin() + i - k, pic.begin() + i, pic[i / k - 1]); } picture.resize(pictu..

article thumbnail
Level 0. 무작위로 K개의 수 뽑기

Level 0. 무작위로 K개의 수 뽑기 배열의 인덱스와 값을 활용해 O(1) 시간에 중복을 확인했다. #include #include #include using namespace std; vector solution(vector arr, int k) { int* p = new int[100'001](); vector answer(k, -1); int i = 0; for (int e : arr) { if (k

article thumbnail
Level 0. 왼쪽 오른쪽

Level 0. 왼쪽 오른쪽 #include #include #include using namespace std; vector solution(vector str_list) { auto it_l = find(str_list.begin(), str_list.end(), "l"); auto it_r = find(str_list.begin(), str_list.end(), "r"); if (it_l < it_r) str_list.erase(it_l, str_list.end()); else if (it_r < it_l) str_list.erase(str_list.begin(), ++it_r); else return vector(); return str_list; }

article thumbnail
Level 0. 리스트 자르기

Level 0. 리스트 자르기 #include #include #include using namespace std; vector solution(int n, vector slicer, vector num_list) { vector answer; int a = slicer[0], b = slicer[1] + 1, c = slicer[2]; switch (n) { case 1: { answer = vector(num_list.begin(), num_list.begin() + b); break; } case 2: { answer = vector(num_list.begin() + a, num_list.end()); break; } case 3: { answer = vector(num_list.begin() + ..

article thumbnail
Level 0. 수열과 구간 쿼리 1

Level 0. 수열과 구간 쿼리 1 #include #include #include using namespace std; vector solution(vector arr, vector queries) { for (vector& v : queries) { int s = v[0], e = v[1] + 1; for_each(arr.begin() + s, arr.begin() + e, [](int& num) { ++num; }); } return arr; }

article thumbnail
Level 0. 특정 문자열로 끝나는 가장 긴 부분 문자열 찾기

Level 0. 특정 문자열로 끝나는 가장 긴 부분 문자열 찾기 string::rfind() 함수를 통해 뒤에서부터 찾는 것이 효율적이다. #include using namespace std; string solution(string myString, string pat) { return myString.substr(0, myString.rfind(pat) + pat.length()); }

검색 태그