Make Unreal REAL.
article thumbnail
Level 2. 뒤에 있는 큰 수 찾기

Level 2. 뒤에 있는 큰 수 찾기 문제를 보면서 생각해보다가 이거 스택으로 풀 수 있겠는데?라는 생각이 들었다. 스택에는 Botton에서부터 큰 수에서 작은 수로 쌓이게 되기 때문에, while문에서 도중에 끊겨 올바른 비교를 하지 못하는 경우는 발생하지 않는다. 이 문제를 풀고나서 내가 이제 자료구조를 쓸 줄 알게 되었구나,, 이제 알고리즘에 감 좀 잡았구나.. 싶으며 다른 사람들의 풀이를 봤는데 다 스택으로 풀었더라. #include #include #include #include using namespace std; vector solution(vector numbers) { size_t size = numbers.size(); vector answer(size, -1); stack stck;..

article thumbnail
Level 2. 덧칠하기

Level 2. 덧칠하기 새로운 vector를 만들어 페인트가 이미 칠해진 곳은 true, 벗겨진 곳은 false로 표시했다. 앞에서부터 false를 찾으면 그 위치부터 m개를 false에서 true로 바꾸며 더 이상 벗겨진 곳이 없을 때까지 반복해 해결했다. #include #include #include using namespace std; int solution(int n, int m, vector section) { int answer = 0; vector paints(n + 1, true); vector::iterator it = paints.begin() + section.front(); for (int sec : section) paints[sec] = false; while ((it = f..

article thumbnail
bool 변수는 레퍼런스 전달이 불가능하다.

Call by reference of bool types How can I change this piece of code to a working one?! #include void foo( bool &b ) // ERROR in passing argument 1 of ‘void foo(bool&)’ { std::cout

article thumbnail
std::replace

std::replace() 함수를 이용해 컨테이너 범위 사이의 값을 변경할 수 있다. #include #include #include using namespace std; void print(vector v); void main() { vector v1{1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4}; vector v2; replace(v1.begin(), v1.end(), 1, 0); print(v1); replace_if(v1.begin(), v1.end(), [](int e) { return e == 2; }, 0); print(v1); replace_copy(v1.begin(), v1.end(), back_inserter(v2), 3, 0); print(v2); replace_co..

article thumbnail
Level 2. 할인 행사

Level 2. 할인 행사 열흘간 want와 discount 배열을 비교할 때 매번 discount에서 각 제품의 개수를 세지 않기 위해서 먼저 다 세어 놓고 시작했다. 각 제품은 모든 1일부터 i번째까지의 개수를 저장하기 때문에 discount 정보를 저장하는 해시맵은 타입으로 지정된다. 개수를 세는 방법은 다음과 같다. 만약 처음 등장한 제품일 경우 배열 크기만큼 0으로 초기화된 벡터를 할당한다. 각 제품은 [i - 1]번째 개수를 [i]번째에 가져온다. 현재 일수에 해당하는 제품의 개수를 1개 증가시킨다. 이를 거치고 나면 umap["banana"][9]에는 1일부터 9일까지 등장한 바나나의 횟수가 저장되어 있다. 만일 둘째 날에 회원가입을 할 경우 제품의 개수는 umap[item][i + 11] ..

article thumbnail
Level 2. 연속 부분 수열 합의 개수

Level 2. 연속 부분 수열 합의 개수 나머지 연산자를 통해 순환시키기보다는 배열을 그대로 뒤에 이어 붙였다. unordered_set을 사용해 중복을 제거했다. #include #include #include #include using namespace std; int solution(vector elements) { size_t size = elements.size(); unordered_set uset; elements.insert(elements.end(), elements.begin(), elements.end()); for (int i = 0; i < size; ++i) for (int j = 0; j < size; ++j) uset.emplace(accumulate(elements.beg..

검색 태그