Make Unreal REAL.
article thumbnail
Level 1. 과일 장수

Level 1. 과일 장수 사과를 점수의 역순으로 정렬한 후 앞에서부터 m개 중 가장 마지막(최저 점수) 사과를 기준으로 가격을 계산하고 해당 m개를 삭제한다. 하지만, 이 방법은 시간 초과가 발생했다. #include #include #include using namespace std; int solution(int k, int m, vector score) { int answer = 0; sort(score.rbegin(), score.rend()); while (m

article thumbnail
Level 1. 가장 가까운 같은 글자

Level 1. 가장 가까운 같은 글자 단순한 방법이라면 이중 for문을 돌면서 자신보다 앞에 있는 같은 글자를 찾을 수도 있지만 O(n²) 시간이 소요되며 비효율적이다. 차례대로 순회하면서 각 문자가 가장 마지막에 등장한 위치를 저장하고 확인할 수 있다면 O(n) 시간이 소요된다. map을 활용해 마지막 등장 위치를 저장하고 확인하여 해결했다. map에서 키의 존재 여부를 확인하기 위해 map.find()나 map.count()를 쓰는 경우도 있는데, map의 구현 스펙을 참고하면 키가 존재하지 않는 경우를 0이나 NULL로 확인하는 방법도 안전하다고 알려져 있다. #include #include #include using namespace std; vector solution(string s) { s..

article thumbnail
Level 1. 폰켓몬

Level 1. 폰켓몬 정렬된 상태를 유지하지 않아도 되므로 set 대신 unordered_set을 사용했다. 중복 포켓몬을 제거한 후 최대 N/2마리의 포켓몬을 가져가면 된다. #include #include #include using namespace std; int solution(vector nums) { return min(unordered_set (nums.begin(), nums.end()).size(), nums.size() / 2); }

article thumbnail
Level 1. 푸드 파이트 대회

Level 1. 푸드 파이트 대회 쉬운 문제였다. #include #include using namespace std; string solution(vector food) { string answer = "0"; for (int i = food.size() - 1; 0 < i; --i) { string sub(food[i] / 2, '0' + i); answer = sub + answer + sub; } return answer; }

article thumbnail
Level 1. [1차] 비밀지도

Level 1. [1차] 비밀지도 지도 1과 지도 2를 각각 계산하여 합칠 필요 없이 OR 연산자로 미리 합칠 수 있다. & 1 연산으로 마지막 자리를 하나씩 가져와 벡터에 담고 transform 함수로 문자열로 변환했다. 벡터나 문자열에 값을 추가하는 도중에 메모리 재할당이 일어나지 않게 하기 위해 필요한 만큼의 크기를 미리 할당했다. #include #include #include using namespace std; vector toBinary(int n, int value) { vector v(n); for (auto rit = v.rbegin(); rit != v.rend(); ++rit) { *rit = value & 1; value >>= 1; } return v; } vector solut..

article thumbnail
Level 1. 콜라 문제

Level 1. 콜라 문제 전에 풀었던 치킨 쿠폰 문제와 비슷한 문제다. #include using namespace std; int solution(int a, int b, int n) { int answer = 0; while (a b ? n - b : 0) / (a - b) * b; }

검색 태그