본문 바로가기

Algorithm

(402)
[프로그래머스] 메뉴 리뉴얼 / substr, 순열과 조합 문제 입출력 예 풀이 #include #include #include #include #include #include using namespace std; map hashMap; //정답 봄 void dfs(int targetnum, string str, string order) { if (str.size() == targetnum) { hashMap[str] += 1; return ; } for (int i = 0; i < order.size(); i++) dfs(targetnum, str+order[i], order.substr(i+1)); return ; } vector solution(vector orders, vector course) { vector answer; //없으면 오류 for (st..
[프로그래머스] 뉴스 클러스터링 / append, find, isalpha, transform 문제 풀이 #include #include #include #include using namespace std; vector v(string str){ char before= tolower(str[0]); vector vec; for(int i=1; i 'z') || (tolower(str[i]) 'z')) { before = tolower(str[i]); continue; } string s; s.append(1, before).append(1, tolower(str[i])); vec.push_back(s); before = tolower(str[i]); } return vec; } int solution(string str1, string str2) {..
[프로그래머스] 괄호 변환 / substr 문제 풀이 #include #include #include using namespace std; bool check(string p); //괄호는 stack으로 해결 string solution(string p) { string answer = ""; string u, v; int count = 0; if(p.size() == 0) return ""; for(int i=0; i
[프로그래머스] 행렬 테두리 회전하기 문제 풀이 #include #include #include using namespace std; vector set_v(int a, int b){ vector v; int n = 1; for(int i=0; i q[2]-1) y--; else x++; } else if(x == q[2]-1){ //
[프로그래머스] 짝지어 제거하기 / stack(LIFO) 문제 풀이 1 #include #include using namespace std; int solution(string s) { int answer = 0, i = 0; while (i < s.length()) { if (s[i] == s[i + 1]) { s.erase(i, 2); i = 0; } else i++; if (s.length() == 0) return 1; } return 0; } while문으로 푼 풀이이다. 정답은 다 맞혔으나 효율성 테스트를 전혀 통과할 수 없었다. 알파벳이 2개인 짝을 제거할 경우 다시 처음부터 탐색하기 때문에 비효율적일수밖에 없었을 것이다. 풀이 2 #include #include #include using namespace std; int solution(stri..
[프로그래머스] 깊이/너비 우선 탐색(DFS/BFS) - 타겟 넘버 문제 풀이 #include #include using namespace std; vector datas; int answer; int tarN; void dfs(int n, int before); int solution(vector numbers, int target) { answer = 0; datas = numbers; tarN = target; dfs(0, 0); return answer; } void dfs(int n, int before){ if(n == datas.size()){ if(before == tarN) answer++; return; } dfs(n+1, before + datas[n]); dfs(n+1, before - datas[n]); return; } dfs를 사용하여 모든 경..
[프로그래머스] 힙(Heap) - 더 맵게 / 우선순위큐(priority_queue) 문제 풀이 #include #include #include //heap 구현 #include // greater using namespace std; int solution(vector scoville, int K) { int answer = 0; priority_queue pq; for(int i =0; i < scoville.size(); i++){ pq.push(scoville[i]); } while(pq.top() < K){ if(pq.size()
[프로그래머스] 단체사진찍기 / DFS, assign, next_permutation 문제 풀이 #include #include #include #include #include // abs using namespace std; int answer; vector names; vector visited; vector conditions; void dfs(int n, string inputstr); bool check(string str); int solution(int n, vector data) { answer = 0; names = {'A', 'C', 'F', 'J', 'M', 'N', 'R', 'T'}; visited.assign(8,false); // vector 초기화 conditions = data; dfs(0, ""); return answer; } bool check(strin..