알고리즘 문제풀기/인프런 강의 정답
-
60. 합이 같은 부분 집합 (아마존 인터뷰 문제 : DFS 완전탐색) [재귀 & 깊이/넓이 우선탐색(DFS, BFS)]알고리즘 문제풀기/인프런 강의 정답 2022. 5. 3. 15:18
나의정답. #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include using namespace std; int arr[11]; int arrSum[1025]; int cnt = 0; bool yes = false; void DFS(int maxLevel, int curLevel, int* a) { if (yes == true) return; int* ar = new int[maxLevel]; int sum = 0; int remainSum = 0; for (int i = 0; i maxLevel) { for (int i = 0; i <..
-
59. 부분집합 (MS 인터뷰 문제 : DFS 완전탐색) [재귀 & 깊이/넓이 우선탐색(DFS, BFS)]알고리즘 문제풀기/인프런 강의 정답 2022. 5. 2. 12:31
나의정답. #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include using namespace std; void DFS(int maxLevel, int curLevel, const int* arr) { int level = curLevel; int* a = new int[maxLevel]; for (int i = 0; i maxLevel) { for (int i = 0; i < maxLevel; i++) { if(a[i] == 1) printf("%d ", i + 1); } printf("\n"); return; } a[curLeve..
-
58. 이진트리 깊이우선탐색 (DFS: Depth First Search) [재귀 & 깊이/넓이 우선탐색(DFS, BFS)]알고리즘 문제풀기/인프런 강의 정답 2022. 4. 29. 19:56
나의정답. 전위순회 #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include using namespace std; void DFS(int node) { if (node > 7) return; else { printf("%d ", node); DFS(node * 2); DFS(node * 2 + 1); } } int main(int argc, char** argv) { //freopen("input.txt", "rt", stdin); DFS(1); } 나의정답. 중위순회 #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include using names..
-
57. 재귀함수(스택)를 이용한 2진수 출력 [재귀 & 깊이/넓이 우선탐색(DFS, BFS)]알고리즘 문제풀기/인프런 강의 정답 2022. 4. 27. 16:03
나의정답. #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include using namespace std; void Digit(int deci) { if (deci == 0) return; else { Digit(deci / 2); printf("%d", deci % 2); } } int main(int argc, char** argv) { //freopen("input.txt", "rt", stdin); int deci; scanf("%d", &deci); Digit(deci); }
-
56. 재귀함수 분석 (스택을 이용하는 재귀) [재귀 & 깊이/넓이 우선탐색(DFS, BFS)]알고리즘 문제풀기/인프런 강의 정답 2022. 4. 27. 15:56
나의정답. #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include using namespace std; void recur(int num) { if (num == 0) { return; } else{ recur(num - 1); printf("%d ", num); } } int main(int argc, char** argv) { //freopen("input.txt", "rt", stdin); int n; scanf("%d", &n); recur(n); }
-
55. 기차운행 (스택 자료구조 응용) [정렬 & 이분탐색(결정알고리즘) & 투포인트 알고리즘 & 스택]알고리즘 문제풀기/인프런 강의 정답 2022. 4. 25. 20:23
나의정답. #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include using namespace std; int main(int argc, char** argv) { //freopen("input.txt", "rt", stdin); int n, i = 1, j = 0, start, count = 0; scanf("%d", &n); stack s; char* a = new char[n * 2]; while (count < n) { scanf("%d", &start); s.push(start); a[j++] = 'P'; while (!s.empty() && s.top() == i) { s.pop(); a[j++] = 'O'; i..
-
54. 올바른 괄호 (STL stack 자료구조 활용) [정렬 & 이분탐색(결정알고리즘) & 투포인트 알고리즘 & 스택]알고리즘 문제풀기/인프런 강의 정답 2022. 4. 25. 10:13
나의정답. #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include using namespace std; int main(int argc, char** argv) { //freopen("input.txt", "rt", stdin); stack s; char c = 'a'; bool no = false; while (c != 10) { scanf("%c", &c); if (c == '(') s.push(c); else if (c == ')' && s.empty()) no = true; else if (c == ')') s.pop(); } if (no || !s.empty()) printf("NO"); else printf("Y..
-
53. K진수 출력 (스택 자료구조 직접 구현) [정렬 & 이분탐색(결정알고리즘) & 투포인트 알고리즘 & 스택]알고리즘 문제풀기/인프런 강의 정답 2022. 4. 24. 10:22
나의정답. #define _CRT_SECURE_NO_WARNINGS #include #include #include #include using namespace std; template class Stack { public: T* arr = new int[10000]; void Push(T num) { arr[Count] = num; Count++; } int Pop() { if (Count > 0) { Count--; return arr[Count]; } return -1; } int GetCount() { return Count; } private: int Count = 0; }; int main(int argc, char** argv) { //freopen("input.txt", "rt", stdi..