코딩테스트/프로그래머스

    [2] JadenCase 문자열 만들기

    #include #include using namespace std; string solution(string s) { string answer = ""; answer+=toupper(s[0]); for(int i=1;i

    [2] 최댓값과 최솟값

    #include #include #include using namespace std; string solution(string s) { vector v; string temp = ""; for (int i = 0; i < s.size(); i++) { if (s[i] == ' ') { v.push_back(stoi(temp)); temp = ""; } else temp += s[i]; } v.push_back(stoi(temp)); sort(v.begin(), v.end()); return to_string(v.front()) + ' ' + to_string(v.back()); }

    [3] 최고의 집합

    #include #include using namespace std; vector solution(int n, int s) { vector answer; if (n > s) answer.push_back(-1); else { int d=s/n, r=s%n; answer.assign(n, d); for (int i = answer.size() - 1; i >= 0 && r > 0; i--) { answer[i]++; r--; } } return answer; }

    [3] 등굣길

    #include #include #define SIZE 101 using namespace std; int solution(int m, int n, vector puddles) { int check[SIZE][SIZE]{0}, vis[SIZE][SIZE]{0}; for(int i=0;i

    [3] 단속 카메라

    #include #include #include using namespace std; int solution(vector routes) { int answer = 1; sort(routes.begin(), routes.end()); int tmp = routes[0][1]; for (auto a : routes) { if (tmp = a[1]) tmp = a[1]; } return answer; }

    [3] 베스트 앨범

    참고 사이트 : 알고리즘(C++) / 프로그래머스 level 3 : 베스트앨범 (tistory.com) #include #include #include #include #include #include #include using namespace std; using StringPair = pair; using IntPair = pair; // 내림차순 정렬 bool CmpStr(StringPair& a, StringPair& b) { return a.second > b.second; } bool CmpInt(IntPair& a, IntPair& b) { return (a.first == b.first) ? a.second b.first; } vector soluti..

    [3] 이중 우선순위 큐

    삭제 추가가 용이한 이중 연결 리스트를 사용했다. #include #include #include #include using namespace std; vector solution(vector operations) { vector answer; list bl, sl; for (const auto& item : operations) { string str = item; str.erase(remove(str.begin(), str.end(), ' '), str.end()); if (str[0] == 'I') { string tmpStr = ""; for (int i = 1; i < str.size(); i++) tmpStr += str[i]; bl.push_back(stoi(tmpStr)); bl.sort(..

    [3] 숫자 게임

    #include #include #include using namespace std; int solution(vector A, vector B) { int answer = 0; sort(A.begin(),A.end()); sort(B.begin(),B.end()); int idxA=A.size()-1,idxB=B.size()-1; while(idxA >= 0) { int valA=A[idxA],valB=B[idxB]; if(valB > valA) { answer++; idxB--; } idxA--; } return answer; }

    [3] 가장 먼 노드

    이 문제에는 함정이 존재한다 따라서 SIZE를 20001로 지정한다. #include #include #include #include using namespace std; #define SIZE 20001 vector vGraph[SIZE]; vector vCount(SIZE); bool vis[SIZE]; void DFS(int start) { queue q; q.push(start); vis[start] = true; while (!q.empty()) { auto front = q.front(); q.pop(); for (int i = 0; i < vGraph[front].size(); i++) { auto val = vGraph[front][i]; if (!vis[val]) { vis[val] = ..

    [3] 섬 연결하기

    #include #include #include using namespace std; vector vIsland(100); bool Cmp(vector a, vector b) { return a[2] < b[2]; } int FindParent(int idx) { if (vIsland[idx] != idx) vIsland[idx] = FindParent(vIsland[idx]); return vIsland[idx]; } int solution(int n, vector costs) { int answer = 0; sort(costs.begin(), costs.end(), Cmp); for (int i = 0; i < n; i++) vIsland[i] = i; for (int i = 0; i < costs...